0

I am trying to write a simple shell script where on running this script it should change to another directory. I want to make it by allocating a variable to the path. vi loc_change

#!/bin/bash
change= "/home/oracle/Public"
cd $change
echo "directory changed"

after doing this I have changed the permissions chmod 777 loc_change and executing this source ./loc_change after executing this I am getting following error.

./loc_change: line 2: /home/oracle/Public: is a directory
directory changed

I have seen this type of decleration in tldp. I am very new to Shell scripting.

1
  • 2
    Don't put a space between the = and the quotation mark Commented Jan 7, 2014 at 8:49

2 Answers 2

1
#!/bin/bash
change="/home/oracle/Public"

remove the space

try,

 #!/bin/bash
 change="/rooot"
 cd $change > /dev/null > 2&>1
 if [ $? -eq 0 ]
 then
 echo "Directory Changed"
 else
 echo "Directory Not changed"
 fi
Sign up to request clarification or add additional context in comments.

3 Comments

haha yeah but I am getting error will do it once I reach minimum time
we can use like "#!/bin/bash change="/rooot" cd $change > /dev/null > 2&>1 if [ $? -eq 0 ] then echo "Directory Changed" else echo "Directory Not changed" fi"
@RanjithkumarT yep, nice improvements. :).
0

There should be no space.

#!/bin/bash
    change="/home/oracle/Public"
    cd $change
    echo "directory changed"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.