Category Archives: bash

Environment Variables

Your environment can affect you.  If it’s raining and you’re sitting outside, well, then, you’re wet.  Sorry about that.  If you’re sitting inside with the air conditioning on, then you’re cool.  Turning on the air conditioning affects your environment, turning it off can make you warm again.  Bash has the same thing, you can turn things on and off again using environment variables.

They’re called variables because they can vary.  How’s that for a spiffy name?  Setting environment variables changes the behavior of your terminal session, they can also contain information about your terminal session as well.

They’re set by using the command export.  It looks like:

Alpha:~ computerlamp$ export VAR=VALUE

If you want to see all the environment variables in your session, just do:

Alpha:~ computerlamp$ export
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000"
declare -x HOME="/home/computerlamp"
declare -x HOSTNAME="Alpha"
declare -x INPUTRC="/etc/inputrc"
declare -x LANG="en_US.UTF-8"
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="computerlamp"
declare -x LS_COLORS=""
declare -x MAIL="/var/spool/mail/computerlamp"
declare -x OLDPWD
declare -x PATH="/usr/local/bin:/bin:/usr/bin:/home/computerlamp/bin"
declare -x PWD="/home/computerlamp"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x TERM="xterm-256color"
declare -x USER="computerlamp"

Let’s talk about a few of those from the list.

  1. USER . That’s my username!  Hi, I’m computerlamp.
  2. PWD  My current directory
  3. OLDPWD My previous directory.  There’s nothing set there because I haven’t done anything in this login aside from sit in my current directory
  4. HOME My home directory.  It’s the same as PWD right now, but it could be different.  It’s always set to my home directory.
  5. SHELL The shell I’m using, right now I’m using /bin/bash
  6. HOSTNAME That’s the hostname of this computer, which happens to be Alpha.

Now let’s talk about that PATH variable.  It’s several directories strung together with colons (:).   The path is all the directories that bash looks in when you want to execute a command.  My list is:

  1. /usr/local/bin/
  2. /bin/
  3. /usr/bin/
  4. /home/computerlamp/bin

When I try to run any command, bash starts by looking in /usr/local/bin/ for the command, then /bin/, next is /usr/bin/, and finally it tries /home/computerlamp/bin. If it can’t find it in any of those, it gives up and tells you that command isn’t available.

Now suppose I have two commands, /bin/Avenge and /usr/bin/Avenge.  One of them calls Tony Stark and the next calls Steve Rogers.

Captain America Civil War Clipart

Of course, at the end of Captain America:  Civil War, they are not getting along.

If I just run Avenge, it’s going to call Tony Stark by default.  If I want to call Steve Rogers, I’ll execute /usr/bin/Avenge.

The bashrc file

In this post we made aliases… now we want to save them in a file; bash has such a file called the .bashrc file.  It’s also known as the bash configuration file.

That period before the name is very important, without it it isn’t the right file.  It also means that the filename is hidden from view when you do a ls.  You have to use the ls -a flag in order to see it.

But this post isn’t about ls, it’s about the .bashrc file.  We’re going to use nano, that useful editor we talked about in the last post, to edit the file.

I’ll start with opening the file in nano:

Editing .bashrc with nano

Editing .bashrc with nano

I did this with the command:

Alpha:~ computerlamp$ nano .bashrc

Now I’ll type those two alias commands that I used before.  I aliased more to less and history to whatIdid:

Adding Aliases to the .bashrc file

Adding Aliases to the .bashrc file

And now we save the file:

Saving .bashrc with nano

Saving .bashrc with nano

Well, so now what?  I edited the file, I added those two lines, and saved it… just don’t forget to exit nano when you’re done with control-X!

Once we created the file, we need to use it.  Otherwise, we just saved our aliases in a file to do nothing.

It’s called sourcing  the file.  The command to do it is:

Alpha:~ computerlamp$ . .bashrc

See that dot before the .bashrc?  Be sure to type it.  That’s how bash knows to use its configuration file.  Also, every time you open up a new terminal, you’ll have those aliases.  And you’ll be able to see them with the alias command.

You can also look at what you’ve got aliased by using:

Alpha:~ computerlamp$ cat .bashrc

The output is exactly the same, no matter which one you use.  It looks like:

alias less='more'
alias whatIdid='history'

The .bashrc has other uses aside from aliases, we’ll talk about more in the future.

Alias … Not the Television Show

Alias was a television show where Sydney Bristow took on various aliases to do spy things and to kick butt while she was at it. But the aliases were important. She had names like Julia Thorne, Kate Jones, Sharleen…all kinds of names she hid behind.
bash has aliases too, but it’s not names to hide behind because you’re spying, it’s more names to hide that long command behind so you don’t have to remember it. Remember in the post about bash history where I said it should be named thingsIdid or whatIdid? Now we can make that happen!

Alpha:~ computerlamp$ alias whatIdid="history"

 

Now when you type whatIdid, you get the results of the history command.

There’s the more command and the  less command. I don’t like the more command but I love the less command. So what do I do to keep from typing more by accident? Well, I don’t stop myself from typing it, I just make it run the less command:

Alpha:~ computerlamp$ alias more="less"

The bash shell has history, so there’s got to be a way for me to see what commands I’ve aliased aside from using that history command (which I could, if I combined it with grep!). And there is… just type:

Alpha:~ computerlamp$ alias
alias whatIdid="history"
alias more="less"

Since I can add them and I can list them, there’s got to be a way to remove them… and there is! It’s the imaginatively named unalias command.

Alpha:~ computerlamp$ unalias more

That removes the alias for more from my list of aliases.

Alpha:~ computerlamp$ alias
alias whatIdid="history"

What happens if I type the wrong thing?

Alpha:~ computerlamp$ unalias jedi
-bash: unalias: jedi: not found

I guess the alias command doesn’t know where Luke Skywalker is hiding at either.  Which is good for him, right?  He can hide from Snoke and Kylo Ren a little longer.

 

History

Remember that teacher who never forgot a single thing in history? Who could not only tell you what General Custer was wearing at his last stand but the grade you got on your last test?

Well, bash, the shell you’re using in your command line has the same thing. It’s called history. How original! You use the command history to see your history. Okay, maybe not original. Maybe it should be called thingsIdid or whatIdid. Anyway, it looks like:

Alpha:~ computerlamp$ history
    1  ls
    2  pwd
    3  cd
    4  history

See?  Those are the last four commands I happened to type.  Bash is like that teacher, it doesn’t forget.

It’s also useful to combine history with a grep:

Alpha:~ computerlamp$ history | grep ls
    1  ls
    5  history | grep ls

So if you forget ‘exactly how did I do that find command?’ you can, well, find it!

Another way in bash to see your history is to hit the up arrow at the command line.  You’ll see previous commands that you executed and can hit enter and run them again.  If I hit the up arrow ↑ I get:

Alpha:~ computerlamp$ history | grep ls

Which, guess what? It’s the command I just typed.

Your history can contain 500 (or more) commands… and that’s a lot to see. To see the last 10 commands you typed, try this:

Alpha:~ computerlamp$ history 10
    4  ls
    5  echo "Stop Hydra" >plans
    6  ls
    7  history
    8  fortune
    9  yes
   10  find . -type f
   11  ps auxw
   12  ps
   13  history 10

See those numbers before each command? They’re useful too. You can either hit that up-arrow until you find the command you’re looking for (boy, that’s boring) or you can do:

Alpha:~ computerlamp$ !8
fortune
You learn to write as if to someone else because NEXT YEAR YOU WILL BE
"SOMEONE ELSE."

Now you don’t have to remember every command flag you’ve ever used, bash does it for you.