Classification and usage of shell commands under Linux

Source: Internet
Author: User
Tags aliases builtin linux shell commands egrep

Guide When you're going to really manipulate your Linux system, there's nothing like a command-line interface that lets you do that. To be a Linux master, you must be able to understand the different types of shell commands and use them correctly under the terminal.

There are several types of commands under Linux, and for a Linux novice, it is important to know the meaning of different commands to use them efficiently and accurately. So in this article, we're going to be covering a variety of different types of Linux shell commands. One important thing to note: The command-line interface is different from the shell, and the command-line interface just gives you a way to access the shell. And the Shell, which is programmable, makes it possible to communicate with the kernel through commands. Below is a list of different types of commands under Linux:

1. Program executables (commands in the file system)

When you execute a command, Linux finds the executable file for this command by searching the directory stored in the $PATH environment variable from left to right. You can view the directories stored in the $PATH as follows:

$ echo $PATH/home/aaronkilik/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/ Local/games

In the above command, the directory

/home/aaronkilik/bin

Will be searched first, followed by the

/usr/local/sbin

And then go on and on. In the search process, the search order is critical. For example, in the/usr/bin directory in the file system of the command $ ll/bin/.
Example output:

Total 16284drwxr-xr-x 2 root root 4096 Jul 16:30./drwxr-xr-x root root 4096 Jul 31 16:29. /-rwxr-xr-x 1 root root 6456 Apr 18:53 archdetect*-rwxr-xr-x 1 root root 1037440 may + 16:15 Bash*-rwxr-xr-x 1 R       oot root 520992 Jan btrfs*-rwxr-xr-x 1 root root 249464 Jan btrfs-calc-size*lrwxrwxrwx 1 root root   5 Jul 16:19 Btrfsck-btrfs*-rwxr-xr-x 1 root root 278376 Jan btrfs-convert*-rwxr-xr-x 1 root root 249464 Jan Btrfs-debug-tree*-rwxr-xr-x 1 root root 245368 Jan btrfs-find-root*-rwxr-xr-x 1 root Roo  T 270136 Jan btrfs-image*-rwxr-xr-x 1 root root 249464 Jan btrfs-map-logical*-rwxr-xr-x 1 root root 245368 Jan Btrfs-select-super*-rwxr-xr-x 1 root root 253816 Jan btrfs-show-super*-rwxr-xr-x 1 root R Oot 249464 Jan btrfstune*-rwxr-xr-x 1 root root 245368 Jan Btrfs-zero-log*-rwxr-xr-x 1 root root 3 1288 Bunzip2*-rwxr-xr-x 1 ROot root 1964536 busybox*-rwxr-xr-x 1 root root 31288 may bzcat*lrwxrwxrwx 1 root root 6 J UL 16:19 bzcmp-bzdiff*-rwxr-xr-x 1 root root 2140 may bzdiff*lrwxrwxrwx 1 root root 6 Jul 31  16:19 Bzegrep-bzgrep*-rwxr-xr-x 1 root root 4877 may bzexe*lrwxrwxrwx 1 root root 6 Jul 31 16:19 Bzfgrep-bzgrep*-rwxr-xr-x 1 root root 3642 may bzgrep*
2. Linux aliases

These are user-defined commands created by the Shell's built-in command alias, which contains other shell commands with options and parameters. The intention was to replace the lengthy command with a novel, short name. The syntax for creating an alias is as follows:

$ alias newcommand= ' Command-options '

You can enumerate all aliases in the system by using the following command:

$ alias-palias alert= ' notify-send--urgency=low-i "$ ([$? = 0] && echo Terminal | | echo error) "" $ (history|tail-n1|sed-e '/' s/^/s*[0-9]/+/s*//;s/[;&|] /s*alert$//'/') ' Alias egrep= ' Egrep--color=auto ' Alias fgrep= ' Fgrep--color=auto ' Alias grep= ' grep--color=auto ' Alias l= ' ls-cf ' Alias la= ' ls-a ' Alias ll= ' ls-alf ' Alias ls= ' ls--color=auto '

To create a new alias in Linux, read the following example carefully.

$ alias update= ' sudo apt update ' $ alias upgrade= ' sudo apt dist-upgrade ' $ alias-p | grep ' Up '

However, the aliases we created above only work temporarily, and they no longer work after the next system startup. You can set a permanent alias in the '. BASHRC ' file as shown below.

After adding, run the following command to activate:

$ source ~/.BASHRC
3. Linux Shell reserved word

In Shell programming,

If, then, FI, for, while, case, ESAC, else, until

And many more words are shell reserved words. As the description implies, they have a special meaning in the shell. You can list all the shell keywords by using the type command shown below:

$ type If then fi in case Esac else untilif are a shell keywordthen is a shell keywordfi are a shell keywordfor is a  Shell Keywordwhile is a shell keywordcase was a shell Keywordesac is a shell keywordelse are a shell keyworduntil is a shell Keyword
4. Linux Shell Functions

A shell function is a set of commands that are executed together within the current shell. Functions are useful for implementing special tasks in shell scripts. The traditional form of writing shell functions in shell scripts is the following:

Function_name () {Command1command2 ...}

Or something like this:

function function_name {Command1command2 ...}

Let's take a look at how to write a shell function in a script named shell_functions.sh.

#!/bin/bash #write a Shell function to update and upgrade installed Packages Upgrade_system () {sudo apt update;sudo apt dis T-upgrade;} #execute Functionupgrade_system

Instead of executing two commands from the command line:

sudo apt update

And

sudo apt dist-upgrade

In the script, we wrote a Shell function Upgrade_system that executes two commands just like executing a single command. Save the file, and then make the script executable. Finally run the Shell function as follows:

$ chmod +x shell_functions.sh$./shell_functions.sh

5. Linux Shell built-in commands

These are the Linux commands built into the shell, so you can't find them in the file system. These commands include PWD, CD, BG, alias, history, type, source, read, exit, and so on, and you can list or check the Linux built-in commands by using the type command shown below:

$ type pwdpwd is a shell builtin$ type CDCD is a shell builtin$ type BGBG was a shell builtin$ type aliasalias is a shell b uiltin$ type historyhistory is a shell builtin
Conclusion

As a Linux user, it is important to know the type of command you are running. I believe that, through the clear, simple and understandable explanations above, including some related instructions, you may have a different "Linux command"

Kind "with a good understanding.

This article was reproduced from: http://www.linuxprobe.com/linux-shell.html

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

Classification and usage of shell commands under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.