I am wondering why cd
does not work in shell script. It is as follows,
#!/bin/sh cd test mkdir $(date +%d-%mm-%Y)
When I run this, I get can't cd to test
cd: 2: can't cd to /test
Why is it like this?
The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.
To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.
when you write "p" on the command line, it will change the directory. Show activity on this post. If you run a bash script then it will operates on its current environment or on those of its children, never on the parent.
$() – the command substitution. ${} – the parameter substitution/variable expansion.
I had the same problem. Turned out the problem was \r\n line endings.
To fix it, do
tr -d "\r" < oldname.sh > newname.sh
From http://talk.maemo.org/showthread.php?s=1cadd53b369d5408c2b9d53580a32dc4&t=67836&page=2
Not really relevant for this question. I had the same error message, however, I was using
cd ~/foo/bar
After changing this to
cd $HOME/foo/bar
it was fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With