Showing posts with label LPIC-1. Show all posts
Showing posts with label LPIC-1. Show all posts

Wednesday, January 18, 2017

Linux: Using Command History

The bash shell supports command history. Every time you enter a command at the shell prompt, that command is saved in the ~/.bash_history file in your home directory. This file is just a simple (hidden) text file that contains all of your previously entered shell commands, one on each line. This file is continually updated each time you enter a shell command. You can display the contents of the .bash_history file by entering history at the shell prompt.
If you press the UP ARROW key at the shell prompt, bash will read this file and display the last command you entered. If you press the UP ARROW key repeatedly, you can scroll through a list of your last-used commands. When you arrive at the one you want, simply press ENTER to execute the command. I love this feature, especially if I need to retype a very long, complex command. Just hit the UP ARROW key until the desired command is displayed and then press ENTER!
If you don’t want to arrow through all of your past commands to find the one you want, you can also enter a part of the command you need and then press CTRL-R. The bash shell will search through your command history and display the most recent matching command.
You can manage the entries in your history file using the following environment variables:
     HISTSIZE or HISTFILESIZE   Configures the size of your history file. On most distributions, this is set to 1,000 entries. You can customize the size of your history file by changing the value of this variable.
     HISTCONTROL   Controls how your command history is stored. You can set this variable to a value of ignoredups, ignorespace, ignoreboth, or erasedups. A value of ignorespace tells the shell to ignore commands in the history that start with spaces. A value of ignoredups tells the shell to ignore duplicate commands in the history. A value of ignoreboth specifies both ignorespace and ignoredups. You can also set this variable to a value of erasedups to remove all duplicate entries in the history file.

Linux: Using the Shell Prompt

As you gain experience with Linux, you’ll discover that it includes some very powerful commands and utilities that you will use over and over. Some of these include the following:


     halt   This command shuts down the operating system but can be run only by the root user.
     reboot   This command shuts down and restarts the operating system. It also can be run only by root.
     init 0   This command also shuts down the operating system and can be run only by your root user.
     init 6   This command also shuts down and restarts the operating system. It also can be run only by root.
     shutdown   This command can be used by root to shut down or reboot the system.
     exit   This command terminates the currently running process, including the current shell session. For example, if you open a terminal session within the Linux GUI and enter exit at the shell prompt, the terminal session is closed. Likewise, if you are working in the CLI and enter exit, the current shell session is ended and you are logged out.
     su   This command switches from the current user to a new user account. For example, if you’re logged in as usera and need to change to user account userb, you can enter su userb at the shell prompt. This command is most frequently used to switch to the superuser root account. In fact, if you don’t supply a username, this utility assumes that you want to change to the root account. If you enter su –,you will switch to the root user account and have all of root’s environment variables applied. When you’re done, enter exit to return to the original user account.
     env   This command displays the environment variables for the currently logged-in user.
     echo   This command is used to echo a line of text on the screen. It’s frequently used to display environment variables. For example, if you wanted to see the current value of the PATH variable, you could enter echo $PATH.
     top   This command is a very useful command that displays a list of all applications and processes currently running on the system. You can sort them by CPU usage, memory usage, process ID number, and which user owns them.
     which   This command is used to display the full path to a shell command or utility. For example, if you wanted to know the full path to the ls command, you would enter which ls.
     whoami   This command displays the username of the currently logged-in user.
     netstat   This command displays the status of the network, including current connections, routing tables, and so on.
     route   This command is used to view or manipulate the system’s routing table.
     ifconfig   This command is used to manage network boards installed in the system. It can be used to display or modify your network board configuration parameters. This command can be run only by the root user.
     uname   This command returns information about your Linux system using several different options, including the following:
     -s   Displays the Linux kernel’s name
     -n   Displays the system’s hostname
     -r   Displays the Linux kernel’s release number
     -v   Displays the Linux kernel’s version number
     -m   Displays the system’s hardware architecture (such as x86_64)
     -p   Displays the processor type
     -i   Displays the hardware platform
     -o   Displays the operating system
     -a   Displays all of this information

Linux: Managing Shell Configuration Files

If you’re using a non-login shell, things are pretty straightforward. The bash shell simply runs /etc/bashrc for system-wide functions and aliases, and then it runs ~/.bashrc from the user’s home directory for user-specific customizations.
If you’re using a login shell, bash first runs /etc/profile and applies the configurations specified in that file. After that, however, things get a little more complex. As you may have noticed, several of the files listed sound like they do exactly the same thing. You’re right, they do. The issue here is that no distribution uses all of these files. For example, a Fedora system uses ~/.bashrc, ~/.bash_profile, and ~/.bash_logout. Alternatively, openSUSE and Ubuntu systems use ~/.bashrc and ~/.profile.
When a login shell is run, the bash shell program searches for configuration files in the following order:
 1.  ~/.bash_profile
 2.  ~/.bash_login
 3.  ~/.profile
It uses the first file it finds and ignores all of the rest. This isn’t much of an issue on SUSE and Fedora. Remember that .bashrc is not read by bash when loading a login shell (although it may be called by .bash_profile or .profile). Therefore, after reading /etc/profile, the bash shell reads .bash_profile on a Fedora system. Likewise, on an openSUSE system bash reads .profile after reading /etc/profile.
The .bash_logout file is used only when you log out of a login shell. Most distributions don’t include this file in users’ home directories by default. However, most distributions do allow individual users to create their own .bash_logout file and customize it with their own commands to be run at logout.

How the Linux Shell Works

To fully understand how the command-line interface works under Linux, you need to understand the concept of a shell. A shell is a command interpreter that allows you to type commands at the keyboard that are sent to the operating system kernel.
Linux enables you to choose from a variety of shells. As with many other aspects of Linux, you can try out several different command-line shells and choose the one that you like the best. Here’s a list of some of the more popular shells:
     sh (Bourne Shell)   The sh shell was the earliest shell, being developed for UNIX back in the late 1970s. Although not widely used on Linux systems, it is still frequently used on UNIX systems.
     bash (Bourne-Again Shell)   The bash shell is an improved version of the sh shell and is one of the most popular shells today. In fact, it’s the shell used by default on most Linux distributions. If you’re using the command-line interface on a Linux system, more than likely you’re using the bash shell.
     csh (C Shell)   The csh shell was originally developed for BSD UNIX. It uses a syntax that is very similar to C programming.
     tsch   The tsch shell is an improved version of the C Shell. It is the default shell used on FreeBSD systems.
     zsh (Z Shell)   The Z Shell is an improved version of the bash shell.
When you first boot your Linux system and log in, your default shell is loaded. You can identify which shell you’re using by entering echo $SHELL at the command prompt. The echo command is used to display text on the screen. Adding $SHELL to the echo command tells echo to display the contents of the SHELL environment variable for the currently logged-in user.
However, you’re not stuck with the default shell. If you want to switch to a different shell, simply enter the shell’s command name at the prompt. For example, if you are currently using the bash shell and want to use zsh instead, simply enter zsh at the prompt. To stop using the new shell and revert back to the original shell, enter exit.

Linux is capable of running multiple shell sessions at once. Each session can run its own programs, all simultaneously. This can be very useful if you have one program running and then need access to the command prompt. With many distributions, such as openSUSE, you simply press ALT-Fx (where x is a number from 2 to 6) to open a new session. For example, to switch to the third alternate console screen, press ALT-F3. You can then return to your original session by pressing ALT-F1.
As with Windows, you can also run terminal sessions within the Linux GUI. This is done by running a terminal program such as Konsole or GNOME Terminal. To run multiple command-line sessions, simply open two or more terminal windows. Each shell session runs its programs independently of the other sessions.
This can also be accomplished in a second way. While you’re within the Linux GUI, press CTRL-ALT-Fx (where x is a number from 1 to 6). This will switch you to a text-based shell prompt. To switch back to your GUI environment, press ALT-F7 (on most distributions).