Store command inside variable

Is it possible to store a command inside a variable?

i want this piece to be stored inside a variable, so i can use it later in a different command

$u | cut -d " " -f 2
var="$u | cut -d " " -f 2"
eval $var

I tried to use eval but I receive this error:

-f 2: command not found

Anyone know if this is possible?

Thanks

It is a really bad idea. It is extremely insecure, dangerous, error-prone(not just malfunctioning but syntax errors out of the blue given unexpected input), difficult to pass values into and out of, and incomprehensible to anyone who didn't write it (and often enough, to the writer, eventually).

Wanting to do so usually means more ordinary methods like functions, variables, and loops have been overlooked.

What exactly is your goal here? What problem are you trying to solve by storing commands?

I Agree with Corona688 on this, buy the way your syntax issue is a quoting problem: double quotes within command and surrounding the var assignment.

var="$u | cut -d \" \" -f 2"

Also, it will assign the value of $u right then, and not get any "new" value when you use it later.

Functions do this. Use a function.

I agree with everything Chubler_XL, Corona688, and neutronscott have said, but just to be clear, the error you are getting is not from the eval ; it is from the command:

var="$u | cut -d " " -f 2"

which sets var to the string $u | cut -d (with $u expanded to the the current contents of the variable u ) and exports var into the environment of the command -f 2 (which the shell did not find on your search path).