Reputation: 30161
I want to something like this in bash:
alias foo='bar="$(echo hello world | grep \"hello world\")"; echo $bar;'; foo
Expected output: hello world
Ouput: grep: world": No such file or directory
The outer quotes have to be single quotes, with double quotes $bar would be empty.
The next quotes have to be double quotes, with single quotes $() wouldn't expand.
The inner quotes could be both type of quotes, but single quotes doesn't allow single quotes inside of them.
How to I achieve this?
Upvotes: 14
Views: 14235
Reputation: 247210
The stuff inside $()
represents a subshell, so you are allowed to place un-escaped double quotes inside
alias foo='bar="$(echo testing hello world | grep "hello world")"; echo "$bar"'
Upvotes: 34
Reputation: 14147
The double quotes around $()
are not necessary:
alias foo='bar=$(echo hello world | grep "hello world"); echo $bar;'
foo
# Output:
hello world
Upvotes: 0
Reputation: 58988
It's a bit unclear what "something like this" means, but the simplest way to achieve what seems to be the point here is a simple function:
foo() {
echo 'hello world' | grep 'hello world'
}
foo
Upvotes: 5
Reputation: 96326
Escape the spaces
alias foo='bar="$(echo hello world | grep hello\ world)"; echo $bar;'
Upvotes: 1