createchange
createchange

Reputation: 117

Echo chained command into clipboard

I have a script that parses a response from AWS IAM, creates a file with the environment variables and stores the following command in the user's clipboard, used to source the environment variables into their shell:

relevant line in script:

echo -e "source $env_file_name" | pbcopy

results in this being the clipboard contents:

source NAME_OF_FILE

This works - when the user runs `pbpaste`, the environment variables are set, and the user has access to resources. However, if I wanted to perform cleanup of that file by storing a chained command in the clipboard, the second command is never reached:

echo -e "source $env_file_name;rm $env_file_name" | pbcopy

I've tried variations such as:

echo -e "source $env_file_name && rm $env_file_name" | pbcopy

When pasting:

~ > `pbpaste`                                                                                                                                                                                          
zsh: command not found: ;rm

It is clearly intepreting the && and ` as string literals, and I am unsure how to get around this.

Upvotes: 0

Views: 477

Answers (1)

Barmar
Barmar

Reputation: 780788

The only processing normally done after expanding a command substitution is word splitting and filename expansion. If you want the process other shell syntax such as ; to separate commands, you have to use eval.

eval "$(pbpaste)"

Upvotes: 1

Related Questions