temp
temp

Reputation: 13

Shell Script to change to another directory using variables in the script

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.

Upvotes: 0

Views: 227

Answers (2)

atupal
atupal

Reputation: 17210

#!/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

Upvotes: 1

Vamsi Krishna
Vamsi Krishna

Reputation: 485

There should be no space.

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

Upvotes: 0

Related Questions