Ceci est une ancienne révision du document !
Starting this issue, I thought we could focus on terminal commands and conquer those. Most of us don’t think twice when typing at a terminal, it’s just there. What is actually happening is that you are typing commands into a shell that is interpreting your commands. By default, Ubuntu ships with bash, but you can install another if you like. I will focus on bash. The reason being, that if you ever get to use servers or containers on the internet, chances are good that it is bash.
For this command and conquer, I want you to keep this page open in your browser: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html
I will not waste your time with the history of shells, the internet is full of that, so let’s jump to the meat and potatoes. If you are reading this and your distribution does not have bash by default, like my new work Mac, then use ‘chsh’ (aka. change shell) to change it to ‘/bin/bash’. Now log out and in for the changes to take effect.
I have to quickly mention that a terminal emulator is not a terminal emulator is not a terminal emulator, though they may look the same.
The quickest way to see any difference is to right click on your terminal and peruse the menu (see bottom left image). My XFCE terminal can have a menu bar and tool bar, but I hide those. So pick one you like and join in the fun.
Everyone is familiar with the cd command and it’s quirky use of cd~. We will expand on that and start with the ~ (tilde) as it is a built in shell command that points to $HOME. This means you can use it to go to other user’s home folders, for example cd ~root , so the syntax is: cd ~<username> (this will not work for root if your distribution does not have a root user, nor will it work for users that have not yet signed in.)
Now lets have some fun, type: cd /usr Then type: cd /var Now type: cd ~ So we just went full circle, wink wink. Now type: cd ~- , to see where you are, type pwd or dirs. (if your prompt does not show you)
Can you figure out what happened? Do it again… What happened? If you have not realised, do it four times.
Let’s have some fun with this. Now because the values our command is reading are stored in a variable, $OLDPWD, we are just retrieving it. So can we change it?
A quick visualization exercise. Imagine the towers of Hanoi, the game. You have three pegs that you have to move rings from the first to the last. Imagine the stack to be one of those pegs. To get to the second ring, you need to remove the first. This is how the stack on a computer works, like one of the pegs. Though the directory stack that we are going to use has some short-cuts. The commands to put stuff onto the peg and remove stuff are: pushd and popd (You may have heard of push and pop in programming) So now we push a directory or two onto the stack: pushd /dev/block and again: pushd /var/apt and type ‘dirs’ to see what you did. Now type: cd ~- What happened? Do it again (cd~-).
Did it go as expected or not? Just like the ‘.’ and the ‘..’ we see in directories, that can be used in other ways, these values we just pushed onto the stack can be used in other ways too. I have 3 saved paths as you can see, they will be in position 0, 1 and 2. Computers start counting at 0 as a rule of thumb. (There are exceptions)
To see what you have, type: echo ~0 and: echo ~1 -Do you see that they count up from left to right?
Now lets use popd to remove one of the paths in the list. Here I will explain a bit how the directory stack differs from the stack inside your CPU. The stack in your CPU works on a last in, first out basis, think towers of Hanoi. Lots of stacks do, but the directory stack, we can reach in and remove, say, the second value. Type: popd +1 and then type: dirs to see what you have. So popd reached over position 0 and removed the second path. You could have said popd +2, basically popd +n-th position, assuming it exists.
Another way to think about this is when you type pwd. Print working directory. You can think of these lists of paths as working directories. We used cd~- to move to the previous working directory. Now I want you to use cd~+ to move to the next working directory.
Okay, now you should have a better understanding , because you were hands-on. Now feel free to go read that page I had you keep open for reference, in the beginning and see if you understand it better.
I hope you learned something this issue.
As always, direct any comments or complaints to: misc@fullcirclemagazine.org