Sorry for this stupid question, but I spent a time to figure out!!
#! /bin/bash
a=/etc
cd $a
Normally, if I assign value for $a variable and then cd $a, it works. But when I create a separate file - it doesn't!
Why does it happen like this?
Sorry for this stupid question, but I spent a time to figure out!!
#! /bin/bash
a=/etc
cd $a
Normally, if I assign value for $a variable and then cd $a, it works. But when I create a separate file - it doesn't!
Why does it happen like this?
You're executing your script using #!/bin/bash which launches the new bash session invisible for you and changes its directory to $a and then exits. You just don't see it.
To achieve what you want, I've slightly modified your script:
$ cat test.sh
#!/bin/bash
a="/etc/"
cd $a
echo $a
$ chmod +x test.sh
And executed it using a . dot before script (or source keyword). It executes script inside of the current bash session:
Result:
user@ubuntu:~/test$ . test.sh
/etc/
user@ubuntu:/etc$