Varun Vu
Varun Vu

Reputation: 315

How to print just the string "value" in bash script using the !! command

This is a bash scripting exercise that I can't seem to figure out. I'm trying to print to standard out "value", and only have that as the output. The catch is that you have to use the !! command, which executes the previous command. For example:

echo "value"
!!
echo "value"
value

is the output when I run those 2 commands. The problem is that it also prints the string literal of the command. How can I get it so just the "value" is printed to stdout?

Upvotes: 2

Views: 693

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84579

I think I understand what you want to do, you want to execute the command:

$ echo "value"

Then using History Expansion (for the last command !!, a Word Designator (for the last word $) and then a Modifier (p to print but not execute the command)

$ !!$:p
"value"

If you need to remove the quotes, you can use the substitution and global modifiers:

$ !!$:p:gs/"//
value

If that's not what you are looking for, just let me know and I'm happy to work with you further.

Upvotes: 3

Related Questions