12

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
  • 1
    While you could eval, usually you make a function. Commented Sep 19, 2013 at 11:11

2 Answers 2

24

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

9

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>

You must log in to answer this question.

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