#1
  1. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    97
    Rep Power
    4

    Simple question about BASH variables


    I am attempting to use a variable in a cd command and it is producing an error that I do not understand.
    To test the problem I created this simple script which I called 'test'.

    Code:
    #!/bin/bash
    BD18Root="~/NetBeansProjects/BOARD18"
    echo 'with variable'
    cd $BD18Root
    echo 'direct call'
    cd ~/NetBeansProjects/BOARD18
    pwd
    The script produces the following output:

    Code:
    rich@rich2:~/NetBeansProjects/BOARD18/makedist$ ./test
    with variable
    ./test: line 3: cd: ~/NetBeansProjects/BOARD18: No such file or directory
    direct call
    /home/rich/NetBeansProjects/BOARD18
    rich@rich2:~/NetBeansProjects/BOARD18/makedist$
    As can be seen, the direct version of the cd command works but the version using the variable [ $BD18Root ] does not.

    What am I doing wrong?
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
    ~ is interpreted by the shell but not when you put it in quotes. With the quotes you've told it to literally go to the "~/NetBeansProjects/BOARD18" directory. It doesn't get interpreted with the cd either because you told it to use the BD18Root variable as the argument.

    Remove the quotes.
  4. #3
  5. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    97
    Rep Power
    4
    Works fine without the quotes. Thanks!!

IMN logo majestic logo threadwatch logo seochat tools logo