5

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?

4

You have to use quotes if the path contains space characters:

FILE_NAME="/home/$USER/Downloads/My Folder"
  • I tried giving the file name within quotes but the error still remains. – logamadi Mar 14 '15 at 20:34
  • 1
    @logamadi The error isn't an issue setting the variable, it's from how you're calling it. If you want to echo the content of the variable, run echo $FILE_NAME. What you were doing is just trying to execute it. And if it's still telling you it doesn't exist, it doesn't exist. – Oli Mar 14 '15 at 21:17
  • 1
    @Rmano,@Oli The commands "ls" and "cd" work when we wrap the User-defined variable within quotes like :cd "$FILE_NAME" (or) ls "$FILE_NAME" Credit to george Barbaz – logamadi Mar 15 '15 at 5:28
2

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"

  • Error still persists – logamadi Mar 14 '15 at 20:42
  • see further edit of my msg ... the command line you are using would try to execute "$FILE_NAME" – george Barbaz Mar 14 '15 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.