1

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?

muru
  • 69,900
  • 13
  • 192
  • 292
gnarlyninja
  • 308
  • 3
  • 8
  • Zsh has "global" aliases. Bash doesn't. Care to switch to zsh? – muru Feb 12 '20 at 06:55
  • welp, its not the first time its been recommended to me, I guess its time haha – gnarlyninja Feb 12 '20 at 06:58
  • https://unix.stackexchange.com/a/493172/70524 – muru Feb 12 '20 at 07:15
  • @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.). http://schilytools.sourceforge.net/man/man1/bosh.1.html – schily Feb 13 '20 at 11:22

3 Answers3

0
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

amisax
  • 2,957
  • 17
  • 23
  • 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 – gnarlyninja Feb 12 '20 at 07:04
0

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
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? – gnarlyninja Feb 12 '20 at 07: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. – Poisson Aerohead Feb 12 '20 at 07: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. – Poisson Aerohead Feb 12 '20 at 07:19
0

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`'
Egon
  • 324
  • 1
  • 7