Linux basics from the founder of Gentoo. Part 3 (4/4): Customizing Your User Environment
- Transfer
Linux basics navigation from the founder of Gentoo:
Part I:
- BASH, the basics of navigation
- Manage files and directories
- Links and deleting files and directories
- Glob lookups
Part II:
- Regular expressions
- Folder assignments, file search
- Process management
- Text Processing and Redirection
- Kernel modules
Part III
- Documentation
- Access model
- Account management
- Environment setting
Customization of user environment
Introducing Fortune
Your environment has many useful options that you can change as you wish. However, so far we have not discussed how to restore these settings each time you log in, except that you have to retype them each time. In this section, we will consider setting up your environment by editing the startup configuration files.
To get started, let's show a friendly message when you log in. To see an example of such a message, run fortune : (fortune application may not be installed, run the installation in the package manager of your distribution, for example apt-get install fortune - approx.
$ fortune
No amount of careful planning will ever replace dumb luck.
.bash_profile
Now let's make fortune run at each authorization. Using your favorite text editor, edit the .bash_profile file in your home directory. If such a file does not exist, create it. Paste it at the beginning: Try logging out and logging back in. Before starting the display manager, such as xdm, gdm or kdm, you will see a funny greeting when you log in:
fortune
mycroft.flatmonk.org login: chouser
Password:
Freedom from incrustations of grime is contiguous to rectitude.
$
The shell of the entrance.
When bash starts, it passes the .bash_profile file in your home directory, launching each line as if typing it on the command line. This is called file sourcing.
Bash can work in different ways depending on how it is launched. If it is run as an login shell, it will work as described above - first processing the system-wide / etc / profile, and then your personal ~ / .bash_profile.
There are two ways to run bash as a login shell. The first is used when you first log in: bash starts with the process name -bash. You can see this in the output of the process list:
$ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
chouser 404 0.0 0.0 2508 156 tty2 S 2001 0:00 -bash
You will probably see a longer list, but it will have at least one line with a dash before the name of your shell, like -bash in the example above. This feature is used by the shell to determine if it was started during authorization.
Understanding --login
The second way to run bash as an login shell is with the –login option. From time to time, this feature is used by terminal emulators (like xterm) to make their bash session look like the initial login.
After authorization, you can run many copies of your shell. For those that are launched without the --login option or do not have a dash in front of the process name, the behavior will be different than when launched with authorization. They provide you with a command line, however, they are called interactive shells. If bash is started interactively without authorization, it will ignore / etc / profile and ~ / .bash_profile will process ~ / .bashrc instead.
interactive login profile rc
yes yes source ignore yes no ignore source no yes source ignore no no ignore ignore
Interactivity Test
Sometimes bash processes your ~ / .bashrc without being launched interactively, for example when using commands like rsh or scp. This is important to remember because text output, as in the example with the fortune command above, can ruin these non-interactive bash sessions. A good idea is to use the PS1 variable to check if the current session is interactive before displaying the text:
if [ -n "$PS1" ]; then
fortune
fi
/ etc / profile and / etc / skel
As a system administrator, you are responsible for the / etc / profile file. Since it is guided by all those who first enter the system, it is important to keep it in working condition. It is also a very powerful tool used to make things work correctly for new users as soon as they log in using their new account.
However, there is an option in which, on the one hand, the settings can be set to the default values for new users, and on the other, they can easily be changed by them if necessary. Just for this the directory / etc / skel exists. When using the useradd command to create a new account, all files from / etc / skel are copied to the new user's home directory. This means that you can put, for example, .bash_profile and .bashrc in / etc / skel for more comfortable starting a new user.
export
Variables in bash can be marked in such a way that they will be set in all newly launched shells. This means that they are marked as external (export). You can force bash to display a list of all the external variables thus labeled in your bash session:
$ export
declare -x EDITOR="vim"
declare -x HOME="/home/chouser"
declare -x MAIL="/var/spool/mail/chouser"
declare -x PAGER="/usr/bin/less"
declare -x PATH="/bin:/usr/bin:/usr/local/bin:/home/chouser/bin"
declare -x PWD="/home/chouser"
declare -x TERM="xterm"
declare -x USER="chouser"
Exporting Variables
If the variable is not marked for export, its value will not be set for newly launched shells. But you can mark the variable for export by passing it to the built-in export command: In this example, both the FOO and BAR variables were set, but only the BAR was marked for export. When the new bash was launched, it lost the value of the FOO variable. If you exit this new bash, you will see that the initial values of both FOO and BAR have not changed.
$ FOO=foo
$ BAR=bar
$ export BAR
$ echo $FOO $BAR
foo bar
$ bash
$ echo $FOO $BAR
bar
$ exit
$ echo $FOO $BAR
foo bar
Export and set -x
In connection with the above behavior, variables can be specified in ~ / .bash_profile or / etc / profile and marked for export, so that in the future there is no need to specify them again. But there are several options that cannot be exported, and therefore they must be set in ~ / .bashrc and in your profile sequentially. These options are configured using the built-in set command: The -x option causes bash to display each command that it is about to execute: This can be very useful for understanding the unexpected behavior of commands when using quotes or similar oddities. To disable the -x option , use set + x . Refer to the man documentation page for all options to the set built-in command.
$ set -x
$ echo $FOO
$ echo foo
foo
Setting variables with "set"
The set command can also be used to set values for variables, but specifying this command itself is optional. The bash command “set FOO = foo” does the same as “FOO = foo”. Resetting the value of a variable is done by the built-in unset:
$ FOO=bar
$ echo $FOO
bar
$ unset FOO
$ echo $FOO
Unset vs. FOO =
This is not the same as setting a variable to an empty value, although it is sometimes difficult to explain. One way to notice this difference is to call the set command without parameters to list all the current variables: Using set without parameters is similar to using the built-in export command, except that set displays all variables, not just those that are external.
$ FOO=bar
$ set | grep ^FOO
FOO=bar
$ FOO=
$ set | grep ^FOO
FOO=
$ unset FOO
$ set | grep ^FOO
Exporting variables to change program behavior.
Often, the behavior of commands can be changed by setting environment variables. As with new bash sessions, running programs from your command line will only see environment variables marked for export. For example, the man command checks the PAGER variable to find out which program to use for paging text. When the PAGER variable is set to less, you will see one page first, and pressing the spacebar will move you to the next page. If you change the PAGER variable to cat, then all text will be displayed immediately, without stopping on pages.
$ PAGER=less
$ export PAGER
$ man man
$ PAGER=cat
$ man man
Using "env"
Unfortunately, if you forget to set PAGER back to less, the man program (like some other programs) will continue to output all the requested text non-stop. If you wanted to set PAGER to cat only once, you could use the env command: In this example, the PAGER variable was used with the cat value in the man program, but the PAGER environment variable itself remained unchanged in the bash session.
$ PAGER=less
$ env PAGER=cat man man
$ echo $PAGER
less
Summary and links
Summary
Well, it's time to congratulate you on the completion of the 3rd part of the guide. You should already know how to find information in the system and online documentation, as well as have a good understanding of the access rights model in Linux, user account management and the environment.
References
Be sure to check out the Linux documentation resources used in this guide, especially the Linux Documentation Project , where you can find various guides, FAQs, and priceless mana pages. Do not forget about Linux Weekly News .
The Linux System Administrators guide is a good addition to this guide. You can also find on the Internet a very useful article by Eric Raymond " Unix and Internet Fundamentals HOWTO ".
Daniel Robbins, on the example of a series of articles, "Bash in the examples", shows how to use programming constructs to write your own scripts. This series (especially parts 1 and 2) is a wonderful preparation for the Level 1 LPIC exam, and also helps to consolidate the concepts that are covered by the sections "Setting Up User Environment":
- Bash by example, Part 1: Fundamental programming in the Bourne-again shell (planned translation)
- Bash by example, Part 2: More bash programming fundamentals (planned translation)
- Bash by example, Part 3: Exploring the ebuild system. (translation planned)
To learn more about the Emacs editor, see the developerWorks tutorial, " Living in Emacs ."
The translation was performed by a collective mind using notabenoid.com . Thanks to the following benoid users (in alphabetical order): kindacute , nekjine , Rich . Special thanks to Alexei Blazhko ([email protected]), as well as to the initiator of the entire series of translations, VBart.
To be continued...
About the authors
Daniel Robbins 
Daniel Robbins is the founder of the Gentoo community and the creator of the Gentoo Linux operating system. Daniel lives in New Mexico with his wife Mary and two energetic daughters. He is also the founder and head of Funtoo , and has written numerous technical articles for IBM developerWorks , Intel Developer Services, and the C / C ++ Users Journal.
Chris houser
Chris Hauser has been a supporter of UNIX since 1994, when he joined the team of administrators at Taylor University (Indiana, USA), where he received a bachelor's degree in computer science and mathematics. He then worked in a variety of areas, including web applications, video editing, UNIX drivers, and cryptographic protection. Currently works at Sentry Data Systems. Chris also contributed to many free projects, such as Gentoo Linux and Clojure, and co-authored The Joy of Clojure .
Aron griffis
Iron Griffis lives in Boston, where he spent the last decade working at Hewlett-Packard on projects such as UNIX network drivers for Tru64, Linux, Xen and KVM virtualization security certification, and most recently, the HP ePrint platform . In his spare time from programming, Ayron prefers to reflect on programming problems while riding his bicycle, juggling with bits, or cheering for the Boston professional baseball team Red Socks.
