Bash : Why are spaces important here ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash : Why are spaces important here ?
# 1  
Old 01-15-2015
Bash : Why are spaces important here ?

Platform details:
Shell: bash
OS : Oracle Linux 6.4 (Same kernel as RHEL )

test1.sh is a very basic script which will loop until count variable reaches 15.

Code:
# cat test1.sh
count=5
while [ $count -le 15 ]
        do
                echo $count
                count=`expr $count + 1`
        done
#


# ./test1.sh
5
6
7
8
9
10
11
12
13
14
15
#
#

test2.sh below is almost same as test1.sh script except that 4 spaces denoted using <SPACE> below are missing and hence it doesn't work.
If you miss any one of these 4 space characters , the script won't work. Why ? I didn't expect this from a modern shell like BASH.

Code:
### the below are spaces in test1
count=5
while [<SPACE>$count -le 15<SPACE>]
    do
        echo $count
        count=`expr $count<SPACE>+<SPACE>1`
    done


# cat test2.sh
count=5
while [$count -le 15] # 2 spaces missing in this line
        do
                echo $count
                count=`expr $count+1` # another 2 spaces missing in this line
        done
#
# ./test2.sh
./test2.sh: line 2: [5: command not found

# 2  
Old 01-15-2015
Because "[" is considered a command the same way ls is a command, not a special shell syntax. "[5" is a valid filename too -- if you had a file named "[5", it would run it, so if you don't mean "[5", don't tell it "[5" -- remember how shell syntax works, "command space arguments".

If you left spaces out of other commands, like lsfilename you certainly wouldn't expect that to work.

Last edited by Corona688; 01-15-2015 at 11:26 AM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-15-2015
[ is not any regular parenthesis like in other programming languages. It's equivalent of the test command. And all the keywords that follow [ are arguments passed to the program [ ; including the ] which [ interprets as the "end of arguments".

[ program may be found in /usr/bin or /bin
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 01-15-2015
Quote:
Originally Posted by John K
Code:
count=`expr $count + 1`


There is no need to use an external command for integer arithmetic in bash (or any other POSIX shell):
Code:
count=$(( count + 1 ))

Or, in bash:

Code:
(( ++count ))

This User Gave Thanks to cfajohnson For This Post:
# 5  
Old 01-20-2015
Thank You CFAJohnson

It seems parantheses ( ) doesn't have the issue with spaces as I managed to use $((count+1)) without any issues.
So, [ character being misinterpreted as a command is not applicable for ( )

Code:
# cat test5.sh
count=5
while [ $count -le 15 ]
do
                echo $count
                count=$((count+1))
done

# ./test5.sh
5
6
7
8
9
10
11
12
13
14
15
#

BTW ..What is ++ operator as in (( ++count )) called ?
# 6  
Old 01-20-2015
Quote:
Originally Posted by John K
BTW ..What is ++ operator as in (( ++count )) called ?
It's known as a pre-increment operator. It means, first increment the value in count and then use it.
On the contrary, there's a post-increment operator ((count++)); which means use it first and then increment it.

It will be clear in the following example:
Code:
[user@host ~]$ x=1
[user@host ~]$ y=$((++x))
[user@host ~]$ echo $x $y
2 2
[user@host ~]$ x=1
[user@host ~]$ y=$((x++))
[user@host ~]$ echo $x $y
2 1
[user@host ~]$

These 2 Users Gave Thanks to balajesuri For This Post:
# 7  
Old 01-20-2015
Thank You balajesuri.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script not parsing file with spaces in path

Hi everyone, I'm trying to write my first ever shell script, the OS is Raspbian. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point, and I have no control over the Windows server at all so... (2 Replies)
Discussion started by: gjws
2 Replies

2. Shell Programming and Scripting

Populating a BASH array with a list of files including spaces-in-the-name

For the record, I already tried telling mgmt and the users to disallow spaces in filenames for this script, but it isn't happening for a number of ID10T-error-based reasons. I have simple list of 3 files in a directory that are named like this: bash-3.2$ ls -1 file* file1 file1 part2... (2 Replies)
Discussion started by: ckmehta
2 Replies

3. Shell Programming and Scripting

Dealing with white spaces in bash scripts

I'm trying to search for all files in directory with particular GID then change the GID to match the UID of each file: #!/bin/sh for i in $(find /dump -gid 200 | sed 's/\ /\\\ /g' | sed 's/\&/\\\&/g'); do chgrp $(ls -ln ${i} | awk '{print $3}') ${i} done I'm using sed to deal with... (7 Replies)
Discussion started by: venmx
7 Replies

4. Shell Programming and Scripting

open application with spaces in name [bash][OSX]

Hi guys, I'm new here and new to shell scripting so don't be hard on me I'm trying to create a bash script to restart a process by name in Mac OSX. I have no problem killing the application, the problem comes when launching it again. I managed to store the path in a variable lets say ... (8 Replies)
Discussion started by: jonathanwiesel
8 Replies

5. Shell Programming and Scripting

Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so: ./RunDiag.js param1:'string one here' param2:'string two here' I have a shell script where I invoke the above command. I can run it in a script as simple as this #!/bin/bash stuff="./RunDiag.js... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

6. Shell Programming and Scripting

Bash - read white spaces

Hello! I have one problem with my bash script - I would like to be able to read white space characters from stdin (for example single " ") - can I acomplish that somehow? I need to read only one character at the time, so I use read -s -n 1 var but it doesn't work for whitespaces apparently. ... (3 Replies)
Discussion started by: xqwzts
3 Replies

7. Shell Programming and Scripting

[BASH] Allow name with spaces (regex)

Hey all, I have a very simple regular expression that I use when I want to allow only letters with spaces. (I know this regex has a lot of shortcomings, but I'm still trying to learn them) isAlpha='^*$'However, when I bring this over to BASH it doesn't allow me to enter spaces. I use the... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies

8. Shell Programming and Scripting

white spaces in bash autocompletion

Hello dear community! I've recently written a BASH function for auto completion of options. It works like following: if a user types a command and then an argument to this command which starts with "^-" and then presses TAB, then 'user_command --help (or -h)' is invoked and possible options are... (0 Replies)
Discussion started by: sidorenko
0 Replies

9. Shell Programming and Scripting

bash replace spaces in list.txt with \

I'm trying to run a Linux virus scan on a list of files/folders I have ported to list.txt in a format: some file with spaces some other file but I need to feed my scanning script in the format: some\ file\ with\ spaces/ some\ other\ file/ so I would like to read in list.txt and output... (6 Replies)
Discussion started by: unclecameron
6 Replies

10. Shell Programming and Scripting

bash - add backslash in front of variables w/ spaces

Hello, Im writing a script that works by recursively going into directories with find. But I have some directories that have spaces in them.. so I need to parse the variables to add a backslash before the spaces. Im not exactly sure how how to do this in bash, and honestly I dont think I know... (3 Replies)
Discussion started by: trey85stang
3 Replies
Login or Register to Ask a Question