Export, clipboard copy and echo a token Bash one-liner
Here’s a handy Bash one-liner to generate a token or some other kind of command output, and do three things at once with it:
- Export it as an environment variable in the shell.
- Copy it to the clipboard.
- Echo it to the terminal.
export FOOBAR_TOKEN=$(foobar-token-command --foobar "foobar") \
&& echo -n $FOOBAR_TOKEN | tee >(xclip -selection clipboard)
I find this is handy for things like security tokens which I often want as an environment variable in the shell as well as being able to paste them into other tools such as HTTP testing apps.
It’s also nice as a quick example of how to use $()
and >()
in Bash.
Note the echo -n
to echo the token without a trailing newline.