using variables on the command line

Following on from my last example of copying a SSH public key to a remote computer, this is something I need to do when setting up a new computer. Setting up private/public keys for SSH just make logging in that little bit smoother.

When you need to rerun the command, you need to load it up, edit it and resubmit it. Unfortunately (although it’s probably possible) I don’t know an easy way to bring up a previous command and edit it in-line so that I can send it again without actually sending the command again before doing so.

Instead, Load a variable into the command line and change it next time.

-- 11:03:01 -- MBP:~ madivad$ ssh minixbmc
Password:
Last login: Mon Apr 25 18:23:18 2016
minixbmc:~ madivad$ exit
logout
Connection to minixbmc closed.
-- 11:03:17 -- MBP:~ madivad$ remote=minixbmc
-- 11:03:26 -- MBP:~ madivad$  history | grep remote
  439  remote=he1000
  440  cat ~/.ssh/id_rsa.pub | ssh madivad@$remote "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
  502  remote=minixbmc
-- 11:03:34 -- MBP:~ madivad$ !440
cat ~/.ssh/id_rsa.pub | ssh madivad@$remote "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Password:
-- 11:03:40 -- MBP:~ madivad$ ssh minixbmc
Last login: Tue Apr 26 11:03:12 2016 from mbp.fritz.box
minixbmc:~ madivad$

For example, in the above┬ásession, for simple commands, I would being the history file up, reissue line 440, then edit, then issue it again. In this situation, it would have the effect of loading the key again, and that’s not what I want to do.

  • Breaking it down, I logged into the remote machine and realised a password was needed,
  • I logged out,
  • I set the “remote” variable,
  • looked for the relevant history command (I knew it had the word “remote” on it),
  • I re-issued that line, and
  • then tested the login.
  • No password was needed, the command was a success.

This could be done with other things as well where you’re always changing one element on the line (or multiple elements, and use multiple variables).

For a more simple and silly example, let’s create a quick update and install script for ubuntu:

upstall=’htop multiwatch’
sudo apt update && sudo apt install $upstall

Instead of typing the whole line next time, I can just type the new apps to install in the “upstall” variable and reissue the command (in this case, using arrow up a couple of times, or grabbing the index from the history file).

$ sudo apt update && sudo apt install $upstall
[sudo] password for madivad:
Hit:1 http://au.archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://au.archive.ubuntu.com/ubuntu xenial-updates InRelease [92.2 kB]
Hit:3 http://au.archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [92.2 kB]
Fetched 184 kB in 1s (101 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done
byobu is already the newest version (5.106-0ubuntu1).
htop is already the newest version (2.0.1-1).
multiwatch is already the newest version (1.0.0-rc1+really1.0.0-1).
0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.

If I then later want do another update and install something else, I can re-set the “upstall” variable and arrow up or grab it out of history.

11:53:44 madivad@he1000:~$ upstall=jq
12:03:44 madivad@he1000:~$ sudo apt update && sudo apt install $upstall
Hit:1 http://au.archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://au.archive.ubuntu.com/ubuntu xenial-updates InRelease [92.2 kB]
Hit:3 http://au.archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [92.2 kB]
Fetched 184 kB in 2s (91.0 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done
jq is already the newest version (1.5+dfsg-1).
0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.
I'm a simple man, I like simplicity. And although there are probably better ways to do this, for the time being, this is how I'm getting the job done. It works well for me, but I'm open to any suggestions and/or improvements.

As I said, not the best example, but hopefully you get the idea.

sudo please

I stumbled across this alias quite a few months ago, but it was under a different name then.

I wanted to find it today and Google searches brought up a whole heap of malicious finds on it, aimed at targeting the unsuspecting newbie to linux. Obviously there are some very dangerous linux commands and so I’m not going to repost them here. Or maybe.

In linux, when you need special permissions to run a command, you’ll get a “permission denied” error. The standard response to that is to “sudo !!” (pron: sudo bang bang) which runs the last command again. Everyone who’s ever used linux will know this can get frustrating.

The I found “fuckit” (or it could have been just “fuck”, I can’t remember which).

When you forget to type sudo, and you’re prompted your permission has been denied, you can issue the expletive whereby the command runs as expected. The funny thing about this, is that when this does happen, it is the sort of thing you are probably thinking, and the expletive does it’s job.

In looking for it today, I found that someone else uses the “please” alias instead. There’s no difference, they both do the same job. It’s just how you feel you want to talk (type) to your computer at the time.

To achieve this, you need to create an alias:

alias please='sudo $(fc -ln -1)'
alias fuck=please

Only include the second line if you feel so inclined. I did because initially you can be all like “please?? Oh thank you understanding computer.” but after a day of permission denied errors, you might be more inclined to just yell at the screen, in which case, the second line is more appropriate.

Creating the alias alone won’t make it persistent. To do that, add the alias to the end of your ~/.bashrc file, or in my case, I’m running ubuntu and you can add it to ~/.bash_aliases instead.