Search code examples
bashdirectoryparentcd

Bash cd .. returns nothing


I have a Bash script:

src="/home/xubuntu/Documents"
mkdir -p "$src/folder1"
src="$src/folder1"

# Do something

printf "SRC IS: $src\n"
src=`cd ..` # RETURN TO PARENT DIRECTORY
printf "SRC IS: $src\n"

Basically I want to create a new folder, then do something inside the folder and after that's done I want to return to the parent directory Documents. For some reason however, src=`cd ..` returns nothing.

SRC IS: /home/xubuntu/Documents
SRC IS: 

Any ideas why?


Solution

  • You can access to the parent :

    src=$(cd ..&&pwd)
    

    Much better and without using cd:

    src=${src%/*} # src is the parent directory