Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

foo=$(ls /usr)

or

foo=`ls /usr`

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

/usr/
/usr/games
/usr/games/cowthink
/usr/games/cowsay
/usr/games/fortune
/usr/games/enemylines7
/usr/games/enemylines3
/usr/lib32
/usr/lib32/libgdbm_compat.so.3.0.0
/usr/lib32/libfusion-1.2.so.9.0.1

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

EXPLANATION:

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.


UPDATE:

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))

See http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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