10

The idea would be to use it as... a pipe in a command. For instance: say there's some kind of long path which has to be retyped again and again, followed by a pipe and a second program, i.e.

"directory1/directory2/direcotry3/file.dat | less -I "

I'd like that part to be stored in a variable, so it could be used like this:

r="directory1/directory2/direcotry3 \| less -I -p "
$ cat path1/path2/$r <searchterm>

Instead, I get

cat: invalid option -- I
Try `cat --help' for more information.

... meaning the pipe clearly didn't work.

  • 1
    While you could eval, usually you make a function. – frostschutz Sep 19 '13 at 11:11
20

bash does not completely re-interpret the command line after expanding variables. To force this, put eval in front:

r="directory1/directory2/direcotry3/file.dat | less -I "
eval "cat path1/path2/$r"

Nevertheless, there are more elegant ways to do this (aliases, functions etc.).

7

You are trying to mix code and data, which is not a good idea. Instead, define a function which takes a file (directory?) name as an argument, and displays it with less.

view () {
    less -I -p "$2" $1
}

view directory1/directory2/directory3 <searchterm>

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.