Unable to understand a line in .bashrc

Unable to understand a line in .bashrc

可是我不能没有你 发布于 2021-11-28 字数 327 浏览 701 回复 4 原文

I found the following code in someone's .bashrc

# Source global definitions 
[ -f /etc/bashrc ] && . /etc/bashrc

It apparently caused my Bash duplicate a lot.

What does the one-liner mean?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍白女子 2022-06-07 4 楼

I'm not sure why you didn't ask here :)
http://comments.pixelbeat.org/settings/

A more verbose equivalent of that command is:

if test -r /etc/bashrc; then
 source /etc/bashrc
fi
动次打次papapa 2022-06-07 3 楼

If /etc/bashrc exists (-f operator) execute it.

That works because the && (Logical AND operator) only evaluates the second argument if the first one is true (to speed up). So if the file exists, run it!

It's actually called an "AND List Construct".

Refer here for some extra info.

You might consider the following commonly used example too:

make && make install

It only installs if make completes successfully.

咋地 2022-06-07 2 楼

What might be confusing you: [ is actually a Unix shell command. You can find it in /bin and when you look more closely, it's a hard link to the test command which means you could also write:

test -f /etc/bashrc && source /etc/bashrc

(. is a shortcut for source).

Historic note for your amusement: When Unix was invented, there were no screens but "teletypes" which is a kind of typewriter. You sent a command to the Unix host and the teletype would print the result. The rumor goes like this: Since the early Unix developers were tired of all the racket the teletypes created, they tried to shorten all the commands to two letters and all options to a single letter.

If you're green, then the rumor is: To save paper and ink ribbon, they tried to shorten all the commands to two letters and all options to a single letter.

And if you look into /dev, you'll see all those tty files which is the only thing that's left from the original teletypes.

[EDIT] How to figure out that that [ and test are the same thing:

This is a two-step process. ls -il [ test echo gives:

356954 -r-xr-xr-x 2 root wheel 46K May 31 2008 [
356954 -r-xr-xr-x 2 root wheel 46K May 31 2008 test
67392  -rwxr-xr-x 1 root wheel 18K Sep 22 2007 echo

First, you notice the "2" before "root". This means that there are two directory entries (a.k.a file names) which point to the same data (Unix lingo: inode, i-node or "index node"). Every inode on the disk has a unique number which is the first number in the example above (356954). You can see that [ and test share the same inode but echo doesn't.

Note that there is no fast way to get all directory entries which point to the same inode. You have to search the whole filesystem for an entry with the same inode (using find /path -inode 356954)

[EDIT2] As for the tty "files": These are in fact device nodes. The first letter of the permissions is "c" as in "character device". You can find "-" for files, "d" for directories and "b" for block devices (like hard disks, floppies, etc). This is not a file but it's a really a program. You can talk to it by sending data to it ("writing" something in the "file") or read data from it. The concept seemed so close to real files that the Unix inventors used a the file API plus something called "IO control" (or ioctl) to send special commands (like querying a terminal for its size).

If you switch to console mode ("Ctrl-Alt-F1"), you'll see something like:

Welcome to .... - Kernel .... (tty1)

Switch to another console (with Alt-F2...Alt-F6) and you'll see they are connected to tty2 to tty6. Login on one of them and try

echo test > /dev/tty1

And the string will appear on the console connected to tty1. Reading from a tty is a not so easy since there is already a program reading from it (either mingetty as long as you're not logged in and login or bash after log in). If you try read line < /dev/tty1, that would conflict with the program which is already running on that console but in principle, it would work.

手心的海 2022-06-07 1 楼

It checks for existence of file /etc/bashrc and executes it if the check is successful. The dot command is an alias for 'source' command.