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 hidden file and simple ls command won’t show the file.
这是一个隐藏文件 ,简单的ls命令不会显示该文件。
To view hidden files, you can run the below command:
要查看隐藏的文件,可以运行以下命令:
$ ls -a
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
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 FileBashrc文件
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
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 directorymkdir:创建目录
- cd : used to change the current directorycd:用于更改当前目录
$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文件已经具有一组预定义的别名。
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命令。
aliaswmi='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的身份运行它。
In general aliases can be defined by adding the statement:
通常,可以通过添加以下语句来定义别名:
aliasaliasname='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提示符将发生变化,如下图所示。
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文件中的HISTSIZE和HISTFILESIZE变量。
- 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文件所做的更改如下所示:
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文件时,用户应注意并始终进行备份,然后再进行任何更改。文章来源:https://www.toymoban.com/news/detail-440424.html
翻译来自: https://www.journaldev.com/41479/bashrc-file-in-linux文章来源地址https://www.toymoban.com/news/detail-440424.html
到了这里,关于Linux中.bashrc文件是什么?的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!