tags:

views:

284

answers:

4

I found the following code in one's .bashrc

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

It apparently caused my Bash duplicate a lot.

What does the one-liner means?

+8  A: 

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.

alkar
Masi
+11  A: 

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.

hvintus
+1 for 'dot' clarification
Mykola Golubyev
+5  A: 

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.

Aaron Digulla
Thank you for the explanation!
Masi
I get the following information for the command by ls -lsa: 96 -r-xr-xr-x 2 root wheel 46K May 31 2008 [. How can you see that it is a hard link to the program test?
Masi
How can you see inside those tty files? I got permission denied -message. I am not sure whether it is safety or not to use sudo.
Masi
@Aaron: nice answer!@Masi: ttys are devices that their names never changed, but functionality did. They are still used as an interface to terminals, although it's not a teletype anymore (try opening a terminal: hit Ctrl+Alt+F1 and login, then type 'tty' -- Ctrl+Alt+F7 will prolly get you back to X)
alkar
@Masi: See my edit
Aaron Digulla
@Aaron: Thank you for your edits! @MacUsers: find -inum is the same as find -inode in Ubuntu.
Masi
Addition to the device nodes. It seems that "l" is for softlinks. For example, I have at my root: - - lrwxr-xr-x@ - -
Masi
@Aaron: Can you clarify the sentence "ls -il [ test echo gives"? I tested many compinations but I did not get what you meant.
Masi
The commands Ctrl-Alt F1 and Alt-F1...F6 are great! I run them in an Ubuntu box. How can you get them to Mac?
Masi
@SQ: Sorry, I forgot to include the output for "echo". And keep in mind this is just an example; you must run the command in the directory where the commands are installed (usually /bin or /usr/bin). The important part is the number of users of the inode and the inode number.
Aaron Digulla
@Masi: I don't think this is available on a standard Mac; the mac has no "console" or "text" mode (Macs always booted into a graphical environment). If you install Linux on your Mac, then you get a console, too, but that's just an emulation.
Aaron Digulla
+2  A: 

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
pixelbeat