linux bash命令

Welcome to our ultimate guide to the Linux Command Line. This tutorial will show you some of the key Linux command line technologies and introduce you to the Bash scripting language.

欢迎使用我们的Linux命令行最终指南。 本教程将向您展示一些关键的Linux命令行技术,并向您介绍Bash脚本语言。

什么是Bash? (What is Bash?)

Bash (short for Bourne Again SHell) is a Unix shell, and a command language interpreter. A shell is simply a macro processor that executes commands. It’s the most widely used shell packaged by default for most Linux distributions, and a successor for the Korn shell (ksh) and the C shell (csh).

Bash (Bourne Again SHell的缩写)是Unix shell,也是命令语言解释器。 外壳程序只是执行命令的宏处理器。 默认情况下,它是大多数Linux发行版中使用最广泛的shell,它是Korn shell(ksh)和C shell(csh)的后继产品。

Many things that can be done Linux operating system can be done via command line. Some examples are…

Linux操作系统可以通过命令行完成许多事情。 一些例子是…

  • Editing files编辑档案
  • Adjusting the volume of the operating system调整操作系统的音量
  • Fetching web pages from the internet从互联网上获取网页
  • Automating work you do every day每天执行的工作自动化

You can read more about bash here, via the GNU Documentation, and via the tldp guide.

您可以在此处通过GNU文档和tldp指南阅读有关bash的更多信息。

在命令行上使用bash(Linux,OS X) (Using bash on the command line (Linux, OS X))

You can start using bash on most Linux and OS X operating systems by opening up a terminal. Let’s consider a simple hello world example. Open up your terminal, and write the following line (everything after the $ sign):

您可以通过打开终端来在大多数Linux和OS X操作系统上开始使用bash。 让我们考虑一个简单的hello world示例。 打开您的终端,并编写以下行($符号之后的所有内容):

zach@marigold:~$ echo "Hello world!"
Hello world!

As you can see, we used the echo command to print the string “Hello world!” to the terminal.

如您所见,我们使用echo命令来打印字符串“ Hello world!”。 到终端。

编写bash脚本 (Writing a bash script)

You can also put all of your bash commands into a .sh file, and run them from the command line. Say you had a bash script with the following contents:

您还可以将所有bash命令放入.sh文件,然后从命令行运行它们。 假设您有一个包含以下内容的bash脚本:

#!/bin/bash
echo "Hello world!"

It’s worth noting that first line of the script starts with #!. It is a special directive which Unix treats differently.

值得注意的是,脚本的第一行以#!开头#! 。 这是Unix对待的特殊指令。

为什么在脚本文件的开头使用#!/ bin / bash? (Why did we use #!/bin/bash at the beginning of the script file?)

That is because it is a convention to let the interactive shell know what kind of interpreter to run for the program that follows. The first line tells Unix that the file is to be executed by /bin/bash. This is the standard location of the Bourne shell on just about every Unix system. Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. #! is often referred to as a “hash-bang”, “she-bang” or “sha-bang”. Though it is only executed if you run your script as an executable. For example, when you type ./scriptname.extension, it will look at the top line to find out the interpreter, whereas, running the script as bash scriptname.sh, first line is ignored.

这是因为让交互式外壳程序知道为随后的程序运行哪种解释器是一种约定。 第一行告诉Unix,该文件将由/ bin / bash执行。 这是几乎每个Unix系统上Bourne shell的标准位置。 将#!/ bin / bash添加为脚本的第一行,告诉操作系统调用指定的shell来执行脚本中后面的命令。 #! 通常被称为“哈希爆炸”,“爆炸”或“爆炸”。 尽管仅当您将脚本作为可执行文件运行时才执行。 例如,当您键入./scriptname.extension ,它将在第一行中查找解释程序,而以bash scriptname.sh身份运行脚本时,第一行将被忽略。

Then you could run the script like so: For make file executable you should call this command under sudo chmod +x “filename”.

然后,您可以像这样运行脚本:为使文件可执行,应在sudo chmod + x“ filename”下调用此命令。

zach@marigold:~$ ./myBashScript.sh
Hello world!

The script only has two lines. The first indicates what interpreter to use to run the file (in this case, bash). The second line is the command we want to use, echo, followed by what we want to print which is “Hello World”.

该脚本只有两行。 第一个指示用于运行文件的解释器(在本例中为bash)。 第二行是我们要使用的命令,echo,然后是我们要打印的“ Hello World”。

Sometimes the script won’t be executed, and the above command will return an error. It is due to the permissions set on the file. To avoid that use:

有时脚本不会被执行,并且上面的命令将返回错误。 这是由于在文件上设置的权限。 为了避免这种使用:

zach@marigold:~$ chmod u+x myBashScript.sh

And then execute the script.

然后执行脚本。

Linux命令行:Bash Cat (Linux Command Line: Bash Cat)

Cat is one of the most frequently used commands in Unix operating systems.

Cat是Unix操作系统中最常用的命令之一。

Cat is used to read a file sequentially and print it to the standard output. The name is derived from its function to concatenate files.

Cat用于顺序读取文件并将其打印到标准输出。 这个名字是从它的功能,骗子 enate文件导出。

用法 (Usage)

cat [options] [file_names]

Most used options:

最常用的选项:

  • -b, numer non-blank output lines

    -b ,非空白的输出行数

  • -n, number all output lines

    -n编号所有输出线

  • -s, squeeze multiple adjacent blank lines

    -s ,挤压多个相邻的空白行

  • -v, display nonprinting characters, except for tabs and the end of line character

    -v ,显示非打印字符,制表符和行尾字符除外

(Example)

Print in terminal the content of file.txt:

在终端中打印file.txt的内容:

cat file.txt

Concatenate the content of the two files and display the result in terminal:

连接两个文件的内容,并在终端中显示结果:

cat file1.txt file2.txt

Linux命令行:Bash cd (Linux Command Line: Bash cd)

Change Directory to the path specified, for example cd projects.

将目录更改为指定的路径,例如cd projects

There are a few really helpful arguments to aid this:

有一些非常有用的参数可以帮助实现这一点:

  • . refers to the current directory, such as ./projects

    . 引用当前目录,例如./projects

  • .. can be used to move up one folder, use cd .., and can be combined to move up multiple levels ../../my_folder

    ..可以用于向上移动一个文件夹,使用cd .. ,并且可以组合用于向上移动多个级别../../my_folder

  • / is the root of your system to reach core folders, such as system, users, etc.

    /是系统访问核心文件夹(例如systemusers等)的根目录。

  • ~ is the home directory, usually the path /users/username. Move back to folders referenced relative to this path by including it at the start of your path, for example ~/projects.

    ~是主目录,通常是路径/users/username 。 通过将其包含在路径的开头,将其移回相对于此路径引用的文件夹,例如~/projects

Linux命令行:Bash头 (Linux Command Line: Bash head)

Head is used to print the first ten lines (by default) or any other amount specified of a file or files. Cat is used to read a file sequentially and print it to the standard output. ie prints out the entire contents of the entire file. - that is not always necessary, perhaps you just want to check the contents of a file to see if it is the correct one, or check that it is indeed not empty. The head command allows you to view the first N lines of a file.

Head用于打印前十行(默认情况下)或一个或多个文件指定的任何其他数量。 Cat用于顺序读取文件并将其打印到标准输出。 即打印出整个文件的全部内容。 -这并非总是必要的,也许您只想检查文件的内容以查看它是否正确,或者检查它确实不是空的。 head命令允许您查看文件的前N行。

if more than on file is called then the first ten lines of each file is displayed, unless specific number of lines are specified. Choosing to display the file header is optional using the option below

如果调用的文件多于文件上的文件,那么将显示每个文件的前十行,除非指定了特定的行数。 使用以下选项选择显示文件头是可选的

用法 (Usage)

head [options] [file_name(s)]

Most used options:

最常用的选项:

  • -n N, prints out the first N lines of the file(s)

    -n N ,打印出文件的前N行

  • -q, doesn’t print out the file headers

    -q ,不打印出文件头

  • -v, always prints out the file headers

    -v ,总是打印出文件头

(Example)

head file.txt

Prints in terminal the first ten lines of file.txt (default)

在终端中打印file.txt的前十行(默认)

head -n 7 file.txt

Prints in terminal the first seven lines of file.txt

在终端中打印file.txt的前七行

head -q -n 5 file1.txt file2.txt

Print in terminal the first 5 lines of file1.txt, followed by the first 5 lines of file2.txt

在终端中打印file1.txt的前5行,然后打印file2.txt的前5行

Linux Command Line: Bash ls

Linux命令行:Bash ls

ls is a command on Unix-like operating systems to list contents of a directory, for example folder and file names.

ls是类似Unix的操作系统上的命令,用于列出目录的内容,例如文件夹和文件名。

用法 (Usage)

cat [options] [file_names]

Most used options:

最常用的选项:

  • -a, all files and folders, including ones that are hidden and start with a .

    -a ,所有文件和文件夹,包括隐藏的文件和文件夹,并以.开头.

  • -l, List in long format

    -l ,以长格式列出

  • -G, enable colorized output.

    -G ,启用彩色输出。

例: (Example:)

List files in freeCodeCamp/guide/

freeCodeCamp/guide/列出文件

ls                                                                ⚬ master
CODE_OF_CONDUCT.md bin                package.json       utils
CONTRIBUTING.md    gatsby-browser.js  plugins            yarn.lock
LICENSE.md         gatsby-config.js   src
README.md          gatsby-node.js     static
assets             gatsby-ssr.js      translations

Linux命令行:Bash man (Linux Command Line: Bash man)

Man, the abbreviation of manual, is a bash command used to display on-line reference manuals of the given command.

人, UAL的缩写,是用于显示给定的命令的上线参考手册bash命令。

Man displays the reletive man page (short for manual page) of the given command.

男子将显示给定的命令的reletive手册页(以下简称 UAL )。

用法 (Usage)

man [options] [command]

Most used options:

最常用的选项:

  • -f, print a short description of the given command

    -f ,打印给定命令的简短描述

  • -a, display, in succession, all of the available intro manual pages contained within the manual

    -a ,连续显示手册中包含的所有可用的入门手册页

(Example)

Display the man page of ls:

显示ls的手册页:

man ls

Linux命令行:Bash mv (Linux Command Line: Bash mv)

Moves files and folders.

移动文件和文件夹。

mv source target
mv source ... directory

The first argument is the file you want to move, and the second is the location to move it to.

第一个参数是您要移动的文件,第二个参数是将其移动到的位置。

Commonly used options:

常用选项:

  • -f to force move them and overwrite files without checking with the user.

    -f强制移动它们并覆盖文件,而无需与用户检查。

  • -i to prompt confirmation before overwriting files.

    -i在覆盖文件之前提示确认。

That's all. Go forth and use Linux.

就这样。 继续并使用Linux。

翻译自: https://www.freecodecamp.org/news/linux-command-line-bash-tutorial/

linux bash命令

linux bash命令_Ultimate Linux命令行指南-Full Bash教程相关推荐

  1. 【Linux/shell】bash命令和sh命令的区别(20210109)

    #注意,linux shell脚本中,首行不用指定bash类型也是可以的哦,默认就是bash,但一般是要标明bash类型的: #即:bash命令和sh命令一般是等效的: [root@centos76 ...

  2. Linux 学习手记(1):命令行BASH的基本操作

    1. Shell 是什么 Shell(壳)是用户与操作系统底层(通常是内核)之间交互的中介程序,负责将用户指令.操作传递给操作系统底层. Shell一般分为:图形化Shell(GUI).命令行Shel ...

  3. Linux命令行–基本的bash shell命令

    启动shell: /etc/passwd:包含系统用户账户列表以及每个用户的基本配置信息 每个条目有七个字段,每个字段用冒号隔开 用户名 用户密码 用户的系统UID 用户的系统GID 用户的全名 用户 ...

  4. linux bash函数里面调用命令行,Linux-在gnome-terminal -x中运行bash函数

    您可以将其与export -f一起使用,就像@kojiro的上面的注释中指出的那样. # Define function. my_func() { // Do cool stuff } # Expor ...

  5. python中执行linux命令(调用linux命令)_Python调用Linux bash命令

    import subprocess as sup  # 以下注释很多(为了自己以后不忘), 如果只是想在python中执行Linux命令, 看前5行就够了 # 3.5版本之后官方推荐使用sup.run ...

  6. Linux 小知识翻译 - 「命令行的提示符」

    这次,聊聊关于「命令行提示符」的相关内容. bash之类的Shell程序是操作Linux所不可缺少的东西.其中bash的提示符也有承担了很重要的作用. 「命令行提示符」的英文是「command pro ...

  7. linux中看挂载的磁盘用什么命令,使用Linux命令行挂载硬盘和分区 | MOS86

    如果您希望更多地使用Linux终端Linux命令行入门快速指南Linux命令行入门快速指南您可以在Linux中使用命令来做很多令人惊奇的事情,而且学习起来并不难. 此外,学习如何手动安装和卸载硬盘是一 ...

  8. 提高Linux效率的30个命令行常用快捷键

    来自:51CTO博客,作者:老男孩oldboy 链接:https://blog.51cto.com/oldboy/2112948 说明 我们经常有时候需要敲命令,但是效率比较低,今天看到一篇非常不错的 ...

  9. linux bash gt,linux之bash的基础特性(一)--gt;命令历史(history命令),命令补全,路径补全...

    关于命令历史-->history 1.与之相关的环境变量:HISTSIZE,HISTFILE-->~/.bash_history,HISTFILESIZE,HISTCONTROL,HIST ...

最新文章

  1. TCP/UDP协议基本概念
  2. chosen.jquery.js 有搜索功能、多选功能的下拉框插件
  3. window平台下 Eclipse Ndk开发中的Method 'NewStringUTF' could not be resolved问题
  4. Redis集群研究和实践(基于redis 3.0.5)
  5. 线性代数笔记:Hadamard积
  6. 头文件包含【预处理】(58)
  7. 上传文件大小限制,webconfig和IIS配置大文件上传
  8. Spring Boot 5:应用程序启动时初始化资源
  9. 安卓APP_ Fragment(2)—— Activity与Fragment的通信
  10. [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)
  11. 线性求逆元模板_专栏:ACM算法面面观[9]逆元
  12. Garbled Circuits介绍 - 56 Yao协议的实现 总结
  13. 图文安装VMware Workstation教程
  14. CSDN如何上传照片
  15. 强制关闭计算机窗口,windows系统怎么取消关机时强制关闭程序提示窗口?
  16. C++华为+阿里+鹅厂面经大盘点-如果换做你能成功吗?
  17. 计网homework
  18. 关于图像opencv中对于长宽的定义
  19. 十进制如何转化成二进制c语言,c语言怎么将十进制转化成二进制
  20. c++ 海康工业相机主动抓拍图片

热门文章

  1. 经典冒泡排序及其优化
  2. 0710 mux协议的作用(ppp拨号时如何和gprs进行at指令交互)
  3. deeplearning.ai 改善深层神经网络 week2 优化算法
  4. Snipaste截图
  5. 转载 JDK + Android-SDK + Python + MonkeyRunner 的安装
  6. netbeans下开发rails快捷键 及 Ruby On Rails开发技巧总结
  7. C语言库函数大全及应用实例四
  8. spring boot redis 分布式锁
  9. Java基于redis实现分布式锁(SpringBoot)
  10. Mac cnpm装包时提示Error: EACCES: permission denied解决办法