If you’re new to Linux, chances are good that the very first thing you’ll notice is the term, "command prompt." You're probably wondering what is a command prompt and what are these different symbols and what do they mean. Don't worry, you're not alone. In this article, I'll clarify the terminology and the symbols for you.
What is the command prompt?
You now know what a terminal emulator is and what a shell is, from my previous article, How to open a Linux Terminal Window. You might need a quick refresher on the two terms. Simply put, a command prompt is an input field in the terminal emulator (CLI) which lets you input/issue commands. The command prompt provides some useful information to the user.
Your command prompt will differ from mine. Look at my command prompt in the screenshot above. My prompt, "[kc@localhost ~] $ " is the text field that provides some information about the Linux system. You’ll see "blinking cursor" followed by the prompt waiting for your commands. I'll further break down this prompt into smaller parts so you can see what they mean.
The text between “”
The text 'kc' (My initials) is the currently logged in username.
The '@' symbol is a separator between the username and the hostname. The 'localhost' designation is my Linux system’s name (hostname) and the default for any newly installed system if you don't specify a hostname during installation. You can change your hostname using the 'hostnamectl' command.
The '~' represents home directory of currently logged in user. This part of the command prompt simply displays your current working directory. My prompt in the image above shows `~` meaning I’m currently working inside my home directory which is '/home/kc'. If you navigate to a different directory this is where you will find where you’re currently working, this is very useful information, so you don’t get lost in the filesystem.
cd to the /etc directory, the prompt becomes: [kc@localhost etc] $. However, if I
cd to the /etc/sysconfig directory, my prompt is displayed as: [kc@localhost sysconfig] $. The full path is not displayed in the prompt but only the current working directory.
The $ means that I'm logged into this system as a regular user. The $ symbol is the default for regular users.
If you’re logged in as the 'root' user, the full prompt changes to [root@localhost ~]#. The # symbol is the prompt designation for the root account.
The general format of the default command prompt is: [username@hostname cwd]$ or #.
Now, since I've covered the basics of command prompt and the meanings of the different parts, I'll show you how you can customize your prompt.
The command prompt is set by the 'PS1' environmental variable, you can see what your current 'PS1' variable is set to by issuing this command:
[kc@localhost ~]$ echo $PS1
The '$' symbol preceding the PS1 variable name is different from the '$' symbol at the end of the prompt. Shell variables are designated by $VARNAME. The $ symbol in front of the variable name is interpreted by the system as a variable. For example, you can set your own variables and recall them from the shell:
[kc@localhost ~]$ a=1 [kc@localhost ~]$ echo $a 1 [kc@localhost ~]$
To view the definition of your command prompt, use the command:
[kc@localhost ~]$ echo $PS1 [\u@\h \W]\$ [kc@localhost ~]$
The [\u \h \W]\$] parts all have special meanings. The
\u returns the username of the current user,
\h returns hostname,
\W returns working directory, and \$ is the user prompt $, which is #, for the root user.
[ Learn more: Linux environment variable tips and tricks ]
Altering the Prompt
You can change your prompt to something else by altering the '$PS1' variable value.
To make this change to your prompt permanent, add the modification to the
.bashrc file in your home directory.
In this brief overview, I've shown you how to interpret your command prompt and how to change it. The command prompt provides very important information about your environment by default but it isn't perfect. Customize it to fit your personal workflow and needs for a more enjoyable command-line experience.
Want to try out Red Hat Enterprise Linux? Download it now for free.