Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Different classifications of Shell commands under Linux and their usage

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "different classifications of Shell commands under Linux and their usage". In daily operation, I believe many people have doubts about the different classifications of Shell commands under Linux and their usage. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "different classifications of Shell commands under Linux and their usage". Next, please follow the editor to study!

The different types of commands under Linux are listed below:

1. Program executable files (commands in the file system)

When you execute a command, Linux finds the executable file for the command by searching the directory stored in the $PATH environment variable from left to right.

You can view the directory stored in $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 / usr/local/sbin, and then continue. In the search process, the search order is very important.

For example, the command in the file system in the / usr/bin directory:

$ll / bin/

Sample output:

Total 16284 drwxr-xr-x 2 root root 4096 Jul 31 16:30. / drwxr-xr-x 23 root root 4096 Jul 31 16:29.. /-rwxr-xr-x 1 root root 6456 Apr 14 18:53 archdetect*-rwxr-xr-x 1 root root 1037440 May 17 16:15 bash*-rwxr-xr-x 1 root root 520992 Jan 20 2016 btrfs*-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-calc-size* lrwxrwxrwx 1 root root 5 Jul 31 16:19 btrfsck-> btrfs*-rwxr-xr-x 1 root root 278376 Jan 20 2016 btrfs-convert*-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-debug-tree*-rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-find-root*-rwxr-xr-x 1 root root 270136 Jan 20 2016 btrfs-image*-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-map-logical*-rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs- Select-super*-rwxr-xr-x 1 root root 253816 Jan 20 2016 btrfs-show-super*-rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfstune*-rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-zero-log*-rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2*-rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox*-rwxr-xr-x 1 root root 31288 May 20 2015 bzcat* lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzcmp-> bzdiff*-rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff* lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzegrep-> bzgrep*-rwxr-xr-x 1 root root 4877 May 20 2015 bzexe* lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzfgrep-> bzgrep*-rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep*

2. Linux alias

These are user-defined commands that are created with the shell built-in command alias and contain other shell commands with options and parameters. The main intention is to replace lengthy commands with novel, short names.

The syntax for creating an alias looks like this:

$alias newcommand='command-options'

You can enumerate all aliases in the system with the following command:

$alias-p alias alert='notify-send-- urgency=low-I "$([$? = 0] & & echo terminal | | echo error)" $(history | tail-N1 | sed-e'\'s / ^\ s * [0-9]\ +\ sposter / [ & |]\ salarm alerts') "'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 can only work temporarily, and they will no longer work after the next system boot. You can set a * alias in the '.bashrc' file as shown below.

Once added, run the following command to activate:

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

3. Linux Shell reserved word

In shell programming, if, then, fi, for, while, case, esac, else, until and more are shell reserved words. As the description implies, they have a special meaning in shell.

You can list all the shell keywords by using the type command shown below:

$type if then fi for while case esac else until if is a shell keyword then is a shell keyword fi is a shell keyword for is a shell keyword while is a shell keyword case is a shell keyword esac is as hell keyword else is a shell keyword until is a shell keyword

4. Linux shell function

A shell function is a set of commands executed together within the current shell. Function facilitates the implementation of special tasks in shell scripts. The traditional form of writing shell functions in shell scripts is as follows:

Function_name () {command1 command2. }

Or like this:

Function function_name {command1 command2. }

Let's see how to write the shell function in a script called shell_functions.sh.

#! / bin/bash # write a shell function to update and upgrade installed packages upgrade_system () {sudo apt update; sudo apt dist-upgrade;} # execute function upgrade_system

Instead of executing two commands from the command line: sudo apt update and sudo apt dist-upgrade, we write a shell function upgrade_system in the script that executes two commands as if it were a single command.

Save the file and make the script executable. Run the shell function as follows:

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

5. Linux Shell built-in commands

These are Linux commands built into 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.

You can list or check the Linux built-in commands with the type commands shown below:

$type pwd pwd is a shell builtin $type cd cd is a shell builtin $type bg bg is a shell builtin $type alias alias is a shell builtin $type history history is a shell builtin so far, the study of "different categories of Shell commands under Linux and their usage" is over. I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report