Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to assign the output of the basename command of a file to the variable "filename". paths is a file that contains the path location, e.g /root/home/david/mynote

$FILE is = mynote. grep is to get the full path /root/home/david/mynote and basename is to extract only the mynote and assign it to the variable filename.

filename=basename `grep $FILE House/paths`

With the above command line, the value of filename is always empty when I enter echo $filename.

share|improve this question

In general, in BASH you save the output of commands to a variable in one of these ways:

foo=$(ls /usr)


foo=`ls /usr`

As for your question, if you have a file called foo.txt whose contents look like this:


then this should do what you need:

while read FILE; do filename=`basename "$FILE"`; echo "$filename"; done < foo.txt

If, instead, you want to search for a specific pattern (e.g. "mynote") in the file do:

grep mynote foo.txt|while read FILE; do filename=`basename "$FILE"`; echo "$filename"; done


while read FILE; do {something}; done < foo.txt

This command reads every line of the file foo.txt and saves its contents into the variable $FILE. It then executes {something} once for each line of the file.

In this case, {something} is this command:

filename=`basename "$FILE"`; echo "$filename"  

It one runs basename on the contents of variable $FILE and saves the result in the variable $filename. Notice the quotes (") around $FILE, they ensure that the command works for file names containing spaces. Finally it uses echo to print the contents of $filename.


After seeing your other question, I understand what you are trying to do. What you need is this:

filename=$(basename `grep $FILE CourseWork/paths`) 
share|improve this answer
or $ filename=$(basename /tmp/foo) – week Dec 3 '12 at 1:07
I want to search for the file-path in with grep in the paths file and the perform the basename "???" then assign it to the variable filename. – ibi Dec 3 '12 at 1:21
@ibi I saw your other question and understood what you are trying to do. See the (re)updated answer. – terdon Dec 3 '12 at 1:54

This is a perfect example why you should stop using the deprecated backtick syntax in bash:

filename=$(basename $(grep $FILE House/paths))


Note, to protect against paths that may contain whitespace, enclose some items in double quotes:

filename=$(basename "$(grep "$FILE" House/paths)")

The reason the variable is empty is this: when the shell sees

filename=basename `grep $FILE House/paths`
  1. the shell first executes the grep command (suppose for illustration the command returns "/foo/bar/myfile")
  2. the shell then attempts to execute "/foo/bar/myfile", temporarily setting the environment variable filename with the value baseline for the duration of that command -- the shell does not contain a filename variable.

This is quietly documented in the bash reference under Environment

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

share|improve this answer
Thanks a million Glenn... !!! Your solution did exactly what I needed. – ibi Dec 3 '12 at 6:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .