Find the answer to your Linux question:
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, ...
  1. #1
    Just 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?

  2. #2
    Just 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}"

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  4. #4
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5
    Quote Originally Posted by konsolebox View Post
    Maybe a different version is called. Try
    Code:
    #!/bin/bash
    echo "$BASH_VERSINFO"
    
    name=gazza
    echo "${name:0:1}"
    Hi,

    On my Ubuntu box, the result was:


    my_bash_script_01.sh: 5: Bad substitution

    -- note no outputting of BASH version; on my other box, the output was:

    2
    g

    -- note BASH version outputted.

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5

    Smile Solved!

    Quote Originally Posted by Cabhan View Post
    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.
    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 !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...