0

I am a beginner in shell scripting. I am trying to change directory in a shell script by passing an absolute path as an argument. I am trying to do it with cd command but it's not working. Any idea how to specify an absolute path? e.g, currently I'm in /home/malik1 and I want to go to /home/documents

What I've done:

if [[ -d "$1" ]]; then

    cd $1

Correct me please if I'm doing it wrong.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Usman Malik
  • 11
  • 2
  • 7
  • 3
    How do you notice it is not working? Could you provide the complete script? You are aware of the fact that a script can only change its working directory, but not of its caller? – glglgl Feb 06 '18 at 15:59
  • 2
    I assume your directory does not happen to have a space in its name, otherwise you'd see an error message upon executing your script. What I mean is that you should quote the variable in the invocation of `cd`, that is, use `cd "$1"`. This prevents errors with variables containing spaces, newline, or other special characters. – Stefan Hamcke Feb 06 '18 at 16:10
  • Isn't the correct path `/home/malik1/documents`? – LMC Feb 06 '18 at 17:12

0 Answers0