I have an alias that is set up like this.

alias X='`xclip -o --selection primary`'

now this works great if I just want to echo my clipboard value. But I would really like to be able to use it as an argument to other commands.

ssh X

I've tried it as a function as well but that doesn't seem to work either. I suppose i could store it just as a string and do ssh $(X) but I would prefer to avoid any syntax like that.

From what I've noticed thus far it doesn't seem like arguments get expanded at all it only seems to work if its the first thing typed.

I mean I know could alias X="ssh xclip..." but I want this to work for every command not just ssh.

So I guess the question is how can I expand a single letter when it is a command argument?

  • Zsh has "global" aliases. Bash doesn't. Care to switch to zsh?
    – muru
    Feb 12 '20 at 6:55
  • welp, its not the first time its been recommended to me, I guess its time haha Feb 12 '20 at 6:58
  • @muru zsh miss uses the name global aliases as this term has been in use since 1980 for begin aliases (the default type of aliases) that are always in effect, while there are local aliases that are in effect only in a specific directory. The correct name for what zsh incorrectly calls global aliases is all expand alias, see bosh`man page (currently page 6ff and page 37 ff.). schilytools.sourceforge.net/man/man1/bosh.1.html
    – schily
    Feb 13 '20 at 11:22

There is a tricky little workaround you can use for chaining aliases, though you'll need to define which commands you want to be able to use it with among your aliases first.

It may not be widely known, but if you just have something like I use:

alias chx='chmod +x'

and then try to run sudo chx you'll get sudo: chx: command not found.

However. If you provide

alias sudo='sudo '
alias chx='chmod +x'

You can now use sudo with this (and any other) alias, as long as the other alias is the first argument.

This can be applied to the programs you will most commonly want to use this clipboard for. To make your supplied example work, for instance:

alias ssh='ssh '
alias X='`xclip -o --selection primary`'
  • Oh boy I do love me some tricky little workarounds, thanks! This is clever and useful. Feb 13 '20 at 22:26
ssh `X` 

should do what you are expecting It will execute the alias X and pass its output to ssh (or an other command) as input arguments

Additionally, you can also use

X | xargs ssh 

which will accomplish the same

  • those are handy might be the best I'll do but I'm looking to avoid any syntax if possible. I also thought of doing while [1] do; export X=xclip..; done Feb 12 '20 at 7:04

The output of any command can be put anywhere into another command (usually as an argument) with "back-ticks" or the "grave accent mark". Is this what you are asking?

[user@domain]$ alias X='ls'
[user@domain]$ X
file1 file2 file3
[user@domain]$ echo X
[user@domain]$ echo `X`
file1 file2 file3

I am not sure if that is what your question meant, but that is how I read it. The output of your alias (which is a command) will be pasted into the new command string (in your example calling ssh). Be advised that sending the clipboard to the argument of ssh does have some security implications.

  • I'm asking how to do what you just said but without backticks, there probably isn't a reasonable solution, le sigh. what security implications? Feb 12 '20 at 7:11
  • The backticks are required because otherwise you would have overwritten the ability to ever pass an 'X' as an argument. In bash, X just means X, while `X` means the output of program (or alias or builtin (which does not exist)) X. You are basically asking to change bash syntax. This is why 'echo ls' will print 'ls' to the terminal while 'echo `ls`' will print the files in the current directory to the terminal. In the context of 'echo ls' the string 'ls' does not refer to the program output. The program output needs the grave accents. Feb 12 '20 at 7:16
  • I am just pointing out that you should be cognizant of where you are copying from if you are pasting it into an ssh command. Feb 12 '20 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.