Customise the command line

By Russell Barnes. Posted

We make Raspbian a little more personal as we get it to behave and look just the way we want it to, as seen in our Command Line Essentials book

Take a look at that blinking cursor on your terminal, and at what’s behind it: pi@raspberrypi ~ $

The $ is known as the ‘dollar prompt’, awaiting your command; before it you see the ~ (tilde), shorthand for ‘home’ - which is /home/pi in this case. Before that is [user name]@[computer name], in the form pi@raspberrypi. Not only is this informative (at least if you’ve forgotten who and where you are), but it’s also something you can change and personalise.

The full article can be found in Conquer the Command Line along with many other great command line tutorials

 You can also buy a physical copy of Conquer the Command Line from our shop!

New user

Let’s start with that user name: pi. If more than one person in your family uses the Pi, you may want to keep the pi user for shared projects, but set up individual login accounts for family members, including yourself. Creating a new user in Raspbian is easy: sudo adduser jo …will create a new user account named jo. You will be prompted for a password (pick a good one) and lots of irrelevant info (dating back to shared university computers of the 1970s) that you can safely ignore by just pressing ENTER at each prompt. Now we have a user account for jo, have a look at /home/jo. Does it look empty? Use ls -A. Jo has never logged into the computer, so you will see the absence of most of the contents of /home/pi for now, such as ~/.gconf , but there is a .bashrc and a couple of other config files.

Not every user has a home directory and logs in: cat /etc/passwd …and you’ll see a lot of users listed that aren’t people. This is because files and programs running on a Unixtype system have to belong to a user (and a group – take a look at /etc/group), as we saw back in chapter 1 when we did ls -l. The user passwords are fortunately not listed in the /etc/passwd file in plain text, so if you want to change a password you’ll need to use the passwd command: sudo passwd jo will change the password for user jo. If you’re logged in as user pi, then simply calling passwd will prompt you to change pi’s password.

Transformations in the virtual world are always easier than those in nature, and this is the case with switching from being ‘pi’ to ‘jo’: we use the change (or substitute) user command, su, like so: su jo. After typing this, you should see the prompt change to jo@raspberry; you can also confirm who you are logged in as with whoami
.

Changing identity

su - jo (note the dash) is usually preferred, as you’ll gain all of jo’s specific environment settings, including placing you in /home/jo. Note that on many other Linux systems, su on its own will enable you to become the root or superuser, with absolute powers (permissions to run, edit, or delete anything). Raspbian (and some other popular GNU/Linux systems like Ubuntu) prefer sudo to run individualprograms with root permissions. Root’s godlike powers may be temporarily attained with sudo -s - try it and note how the prompt changes - but it’s generally a bad idea to run with more permissions than you need, for the same reason it’s a bad idea to run with scissors! For any user, you can customise elements of their command-line use most simply by editing ~/.bashrc. Take a look through that configuration file now: more ~/.bashrc. Note a number of variables in all capital letters, such as PATH, HISTSIZE, and PS1. The last of these controls the prompt you see, currently jo@raspberry ~ $. To change it (for the duration of your current terminal session), try something like: export PS1=”tutorial@magpi > “ This is a temporary change: type exit and you’ve left the su value of jo, so you’ll see pi@raspberry ~ $ once more. If you su back to jo, the magpi prompt will still be gone. To make your change permanent, you need to put the PS1 value you want into ~/.bashrc. A search around the web will bring up many fancy options for better customising the Bash prompt.

The ~/.bashrc file is read upon each login to a Bash session, or in other words, every time you log into a console or open a terminal. That’s unless you change Raspbian’s default shell away from Bash, something you may have reason to do in the future - there are interesting alternatives available for extra features or for smaller memory footprint - but let’s not worry about that for now. You can put all sorts of commands in there to personalise your environment: command aliases are great for regularly used combinations.

Alias

See what’s already there with: grep alias ~/.bashrc There are a few already in there, particularly for the ls command. One entry is: # alias ll=’ls -l’. This sounds quite useful, although the # indicates that it is ‘commented out’, which means that it will not be read by Bash. Open .bashrc in your text editor; the simple text editor from the Accessories menu will do for now, as although we’ve touched on using nano for editing text from the command line, we aren’t going to go into this in detail until the next chapter. Removing the # will mean that now when you type ll, you’ll get the action of running ls -l. Handy, but we could make it better. Change it to: alias ll=’ls -lAhF’… and you’ll get an output in KB or MB, rather than bytes, along with trailing slashes on directory names and the omission of the ever present . and .. (current and parent) directories. Changes take effect after you next start a Bash session, but you can just run that alias as a command (below). To disable an alias for a session, use: unalias ll

 You can customise the commands and shortcuts

Key point

We’ll end with the very first thing many users need to change: the keyboard map. The system-wide setting is in /etc/default/keyboard, but often you need to change it just for individual users. If £ signs and letters without accents are not sufficient for them, log in as the user who wants a different keyboard, or add sudo and the correct path to the commands below. For example, for a Greek keyboard:

touch ~/.xsessionrc
echo “setxkbmap el” > ~/.xsessionrc

Replace el with pt, us, or whatever language you desire. Note that config file we created - .xsessionrc – it holds settings that are read when we start the GUI with startx, so the keyboard setting will cover not just the terminal, but every app used in the session.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.