Let's say I have a text file that has three lines. Its called a
1
2
3
Is there a way to make this possible:
$ x='cat a | head -1'
Let's say I have a text file that has three lines. Its called a
1
2
3
Is there a way to make this possible:
$ x='cat a | head -1'
What you're trying to do is called command substitution, which puts the output of a command onto the command-line of another. e.g. to provide arguments for the other command, or to assign the output to a variable.
You almost had the right syntax. You need to use $()
around your command. e.g.
x=$(cat a | head -1)
or, since the cat is unnecessary, x=$(head -1 a)
backticks (`) can also be used but are considered obsolete, as they have a number of problems (including inability to nest them, and difficulty in distinguishing them from single-quotes).
x
? – steeldriver Jan 27 '18 at 04:18x=$(cat a | head -1)
or, since thecat
is unnecessary,x=$(head -1 a)
– cas Jan 27 '18 at 05:04