#!/bin/bash#echo $PWDcd /home/<my username>/<long path>echo $PWDWhat I get when executing it with bash script.sh:/home/<my username>: No such file or directorye/<my username>/<long path>/home/<my username>Or with bash . script.sh.: .: is a directoryIt looks like in first case it has just skipped first 4 characters (/hom) of the address line for no reason.And in second case, what the hell is .: .:? It's absolutely ungooglable.And ofc when I copypaste this line cd /home/<my username>/<long path> in terminal it works like it should.EDIT: IT WAS ALL ABOUT ONE MISSING SPACE SYMBOL AT THE END OF THE PATH, THANK YOU.
one simple little cd doesn't work in script
bash;scripting
You should check the script for any hidden characters before your /home part (that would explain it can't cd to it, and why the display truncates part of it too). For example:  cat -ve THEFILE  # -e will mark each end of line with a $ and -v will show some of the control characters in the form ^x, ex: ^M for the control character Carriage Return.Fix it: For this, type a working example in your shell, then copy it using the mouse, and edit the script, delete the line, and paste the one you copied in its place. (in vi: if the script is exactly as described (and the faulty line is line 4) : you go to line 4 with 4G, then delete that line with dd, and go in Insert mode on the line above with: O (capital o). Then you can paste the lines you copied with the mouse. then Escape to go back to command mode, and :wq to write the changes it the file and quit vi.You may want to compare the output $(pwd) with the value $PWD: try to replace the : echo $PWD with :  pwd ; echo PWD=$PWDfinally : bash . script.sh should be: bash ./script.sh . The one you typed ask bash to execute . with the argument script.sh, and . being a directory, bash complains. When invoked in this way, the complaint is usually on the form: program_name: some message  . Here bash tries to execute the program ., so its error message mistakenly use .: as the program name prompt, and the message it displays is .: is a directory, indicating that it couldn't execute it and why (it is a directory, not a bash script).Note that when you invode script.sh in this way (bash ./script.sh), you ask your current shell to invoke a bashsubshell that will execute script.shand exit. Only that bash subshell will be: echoing PWD, then cd-ing to the directory, the echoing the new PWD. When that bash exits, your current shell is still in the original directory. If you want to have a file making changes in your current shell, source it instead: . ./script.sh or in bash you also can source ./script.sh (note: . is the more portable way to source a file) (note 2: having a path for the file to source is recommended in recent shell, ie: . script.sh may work too, but it is recommended to specify the local path such as: . ./script.sh)