Results 1 to 5 of 5
Hi,
(Using 2.6.31-14-generic #48-Ubuntu SMP i586 GNU/Linux.)
I made this script (GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)):
#!/bin/bash
name=gazza
echo ${name:0:1}
but got the following error:
my_bash_script_01.sh: 4: Bad substitution
yet, ...
- 08-16-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 5
Odd BASH.
Hi,
(Using 2.6.31-14-generic #48-Ubuntu SMP i586 GNU/Linux.)
I made this script (GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)):
#!/bin/bash
name=gazza
echo ${name:0:1}
but got the following error:
my_bash_script_01.sh: 4: Bad substitution
yet, when I run the commands straight on the command line like this:
name=gazza; echo ${name:0:1}
I get this:
g
which is the right answer. I made the same script on another computer (2.6.3-7mdk #1 i586 unknown unknown GNU/Linux) (GNU bash, version 2.05b.0(1)-release (i586-mandrake-linux-gnu)) and ran it and it worked perfectly. What could be wrong?
- 08-17-2010 #2Just Joined!
- Join Date
- Jul 2006
- Posts
- 6
Maybe a different version is called. Try
Code:#!/bin/bash echo "$BASH_VERSINFO" name=gazza echo "${name:0:1}"
- 08-17-2010 #3
How are you executing the Bash script? If you use "sh SCRIPT" instead of "bash SCRIPT", it will execute as though it is running under the Bourne shell instead of Bash, which may not support substrings.
DISTRO=Arch
Registered Linux User #388732
- 08-17-2010 #4Just Joined!
- Join Date
- Aug 2010
- Posts
- 5
- 08-17-2010 #5Just Joined!
- Join Date
- Aug 2010
- Posts
- 5
Solved!
Hi,
On Ubuntu box, I had been executing script like this:
sh my_bash_script_01.sh
I tried your suggestion, executing like this:
bash my_bash_script_01.sh
and got the following result:
4
g
-- success! I did a 'which sh' and it returned: '/bin/sh', so I did a 'ls -l /bin/sh' and got:
/bin/sh -> dash
-- a symlink to DASH! Another shell interpreter and which I'd never heard of; how many are there?
I've just realised that, when I execute '[command interpreter] [script]', it doesn't matter what the 'hash-bang' is at the top of the script (such as '#!/bin/bash'); the script is executed with the command interpreter specified on the command line. So, when I then did this:
./my_bash_script_01.sh
everything worked perfectly, since I hadn't specified a command interpreter on the command line; the script had to be executed with what the 'hash-bang' specified.
Thanks all
!


Reply With Quote
