博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Linux中什么是.bashrc文件?
阅读量:2531 次
发布时间:2019-05-11

本文共 5222 字,大约阅读时间需要 17 分钟。

The .bashrc file is a script file that’s executed when a user logs in. The file itself contains a series of configurations for the terminal session. This includes setting up or enabling: coloring, completion, shell history, command aliases, and more.

.bashrc文件是用户登录时执行的脚本文件。该文件本身包含终端会话的一系列配置。 这包括设置或启用:着色,完成,shell历史记录,命令别名等。

It is a and simple won’t show the file.

这是一个 ,简单的不会显示该文件。

To view hidden files, you can run the below command:

要查看隐藏的文件,可以运行以下命令:

$ ls -a
Ls A Command

You can see the .bashrc command in the first column. The contents of .bashrc can be changed to define functions, command aliases, and customize the bash.

您可以在第一列中看到.bashrc命令。 可以更改.bashrc的内容以定义函数,命令别名和自定义bash。

.bashrc file has a lot of comments that makes it easy to understand.

.bashrc文件包含很多注释,使它易于理解。

To view the bashrc file:

要查看bashrc文件:

$ cat .bashrc
Cat Bashrc

A few examples of editing .bashrc are provided below.

下面提供了一些编辑.bashrc的示例。

在bashrc中定义函数 (Defining functions in bashrc)

bashrc can be used to define functions that reduce redundant efforts. These functions can be a collection of basic commands. These functions can even use arguments from the terminal.

bashrc可用于定义减少冗余工作的功能。 这些功能可以是基本命令的集合。 这些函数甚至可以使用终端中的参数。

Let’s define a function that tells the date in a more descriptive manner.

让我们定义一个以更具描述性的方式告诉日期的函数。

First you’ll need to enter the .bashrc file in editing mode.

首先,您需要在编辑模式下输入.bashrc文件。

$ vi .bashrc
Bashrc File
Bashrc File
Bashrc文件

This is what the terminal will look like. To start editing press any letter on the keyboard. At the end of the file add the following code:

这就是终端的外观。 要开始编辑,请按键盘上的任意字母。 在文件末尾添加以下代码:

today(){    echo This is a `date +"%A %d in %B of %Y (%r)"` return}

Press escape. Then to save and exit from vi, press colon (:) followed by ‘wq’ and enter.

按Escape键。 然后要保存并退出vi,请按冒号(:),然后按“ wq”并输入。

The changes are saved. To reflect the changes in the bash, either exit and launch the terminal again.

更改已保存。 要反映bash中的更改,请退出并再次启动终端。

Or use the command:

或使用命令:

$ source .bashrc

To run the function just created call today :

要运行刚刚创建的函数,请立即致电:

$ today
Today

Let’s create another function. This would combine the process of creating a directory and then entering that directory into a single command.

让我们创建另一个函数。 这将合并创建目录然后将目录输入到单个命令中的过程。

In the bashrc file add:

在bashrc文件中添加:

mkcd (){  mkdir -p -- "$1" && cd -P -- "$1"}

This combines the two separate commands :

这结合了两个单独的命令:

  • mkdir : creates a directory

    mkdir:创建目录
  • cd : used to change the current directory

    cd:用于更改当前目录

$1 represents the first parameter passed along with the function call.

$ 1表示与函数调用一起传递的第一个参数。

To use this function:

要使用此功能:

$ mkcd directory_name

This command will pass ‘directory_name’ as the parameter.

该命令将传递“ directory_name”作为参数。

Our function will first use mkdir to create the directory by the name ‘directory_name’ and then cd into ‘directory_name’.

我们的函数将首先使用mkdir以名称“ directory_name”创建目录,然后使用cd进入“ directory_name”。

在.bashrc中定义别名 (Defining aliases in .bashrc)

Aliases are different names for the same command. Consider them as shortcuts to a longer form command. The .bashrc file already has a set of predefined aliases.

别名是同一命令的不同名称。 将它们视为较长格式命令的快捷方式。 .bashrc文件已经具有一组预定义的别名。

Aliases 1 1

As a user, if there is an alias that you use regularly, then instead of defining it every time you open the terminal, you can save it in the .bashrc file.

作为用户,如果您经常使用别名,则可以将其保存在.bashrc文件中,而不是每次打开终端时都定义别名。

For example, we can replace the whoami command with the following line of code.

例如,我们可以用以下代码行替换whoami命令。

alias wmi='whoami'

Don’t forget to save the edit and then run:

不要忘记保存编辑,然后运行:

$ source .bashrc

Now I can use wmi command and the terminal will run it as whoami.

现在,我可以使用wmi命令,终端将以whoami的身份运行它。

Whoami 2

In general aliases can be defined by adding the statement:

通常,可以通过添加以下语句来定义别名:

alias aliasname='commands'

Here it is noteworthy to mention that there should be no space between ‘aliasname’, ‘=’ and ‘commands’.

这里值得一提的是,“别名”,“ =”和“命令”之间不应有空格。

Aliases can also be used to store lengthy paths to directories.

别名也可以用于存储目录的冗长路径。

定制终端 (Customizing the terminal )

There are a lot of ways to customize the terminal using bashrc file.

有很多方法可以使用bashrc文件来自定义终端。

To change the text displayed at the prompt, add the following line at the end of the file :

要更改提示符下显示的文本,请在文件末尾添加以下行:

PS1="JournalDev> "

Save the edit and run :

保存编辑并运行:

$ source .bashrc

Once you refresh the bashrc file using the source command, your bash prompt will change like the image below.

使用source命令刷新bashrc文件后,您的bash提示符将发生变化,如下图所示。

Changing Prompt

You can also change the limit of command history that is displayed when the UP arrow is pressed. To do so, change the HISTSIZE and HISTFILESIZE variables in the bashrc file.

您还可以更改按UP箭头时显示的命令历史记录的限制。 为此,请更改bashrc文件中的HISTSIZEHISTFILESIZE变量。

Command History
  • HISTSIZE is the number of commands stored in the memory when bash is running.

    HISTSIZE是bash运行时存储在内存中的命令数。
  • HISTFILESIZE is the number of commands stored on the disc.

    HISTFILESIZE是光盘上存储的命令数。

尾注 (Ending notes)

The changes made to bashrc file look like this:

对bashrc文件所做的更改如下所示:

Bashrc Changes

Redundant command sequences can be put in bashrc under a function. This will save a lot of time and effort. While editing the bashrc file, users should be careful and always take a backup before making any changes.

冗余命令序列可以放在一个函数的bashrc中。 这样可以节省大量时间和精力。 在编辑bashrc文件时,用户应注意并始终进行备份,然后再进行任何更改。

翻译自:

转载地址:http://ktlzd.baihongyu.com/

你可能感兴趣的文章
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>