How to assign File/Folder path to a Variable in Terminal
Say I have the following path:
/home/$USER/Downloads/My Folder
I tried to assign the above given File-Path to a variable FILE_NAME
as follows,
FILE_NAME=/home/$USER/Downloads/My Folder
$FILE_NAME
The output was:
bash: /home/$USER/Downloads/My :No such file or directory
How to resolve this issue?
You have to use quotes if the path contains space characters:
FILE_NAME="/home/$USER/Downloads/My Folder"
The issue is the embedded blank in the name. The simplest way to resolve this issue is to enclose the full path string with quotes (i.e. FILE_NAME="/home/${USER}/Downloads/My Folder"
The reason to use "
in your case is because of your use of $USER which requires a substitution, with '
this would not occur.
A secondary question is how are you going to use the variable. In your example... I would assume that you dropped the cd from the command however ... to use the variable ... you should probably also use " around its use
so ... my guess at your use ... cd "$FILE_NAME"