Recommended Posts

below is a bash script, I copied for a book(slightly changed)

for FILE in $HOME/*
do
    cp $FILE ${HOME}/new/directory
    chmod a+r ${HOME}/new/directory/${FILE}
done

I understand that the first line sets the variable FILE to each of the values contained in $HOME

The third line is confusing me , why is HOME in these {}, like this {HOME} what is that accomplishing, specifically what do those backets do.

Edited by shanenin
Link to post
Share on other sites

Shanenin,

Basically what it is doing is using the value of the variable HOME. The braces keep the variable name seperate from the rest of the path to ensure that the shell doesn't treat the whole thing as the variable name.

crrj.

Link to post
Share on other sites

sure that makes sence, but why not do it to this line also

for FILE in $HOME/*

is there a logical reason why the person who made this script did not use braces in all instances of the variable HOME?

Edited by shanenin
Link to post
Share on other sites
is there a logical reason why the person who made this script did not use braces in all instances of the variable HOME?

You'd have to ask them :) It wouldn't surprise me if it was habitual and the author didn't notice the inconsistency.

Strictly there was no need to use braces at all in that script. In the absence of braces the shell is supposed to use the longest valid variable name. '/' isn't a valid character in a name, so there's no chance of misparsing the whole path as a variable. Still, the braces are the recommended form.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...