Never realized that you could do this until just now:
: >> file
It seems to be functionally similar to:
touch file
Is there a reason why most resources seem to prefer touch over this shell builtin?
filesshelltouch
Never realized that you could do this until just now:
: >> file
It seems to be functionally similar to:
touch file
Is there a reason why most resources seem to prefer touch over this shell builtin?
.bashrc
and .bash_profile
are NOT scripts. They're configuration file which get sourced every time bash
is executed in one of 2 ways:
The INVOCATION section of the bash man page is what's relevent.
A login shell is one whose first character of argument zero is a
-
, or one started with the--login
option.An interactive shell is one started without non-option arguments and without the
-c
option whose standard input and error are both connected to terminals (as determined byisatty(3))
, or one started with the-i
option. PS1 is set and$-
includesi
ifbash
is interactive, allowing a shell script or a startup file to test this state.The following paragraphs describe how
bash
executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section.When bash is invoked as an interactive login shell, or as a non-interactive shell with the
--login
option, it first reads and executes commands from the file/etc/profile
, if that file exists. After reading that file, it looks for~/.bash_profile
,~/.bash_login
, and~/.profile
, in that order, and reads and executes commands from the first one that exists and is readable. The--noprofile
option may be used when the shell is started to inhibit this behavior.When a login shell exits, bash reads and executes commands from the file
~/.bash_logout
, if it exists.When an interactive shell that is not a login shell is started, bash reads and executes commands from
~/.bashrc
, if that file exists. This may be inhibited by using the--norc
option. The--rcfile file
option will force bash to read and execute commands from file instead of~/.bashrc
.
You can control when they get loaded through the command line switches, --norc
and --noprofile
. You can also override the location of where they get loaded from using the --rcfile
switch.
As other's have mentioned you can mimic how these files get loaded through the use of the source <file>
command or the use of the . <file>
command.
It's best to think of this functionality as follows:
This topic seems to come up every once in a while, so here's a little cheatsheet of the various ways to invoke bash
and what they result in. NOTE: To help I've added the messages "sourced $HOME/.bashrc" and "sourced $HOME/.bash_profile" to their respective files.
basic calls
bash -i
$ bash -i
sourced /home/saml/.bashrc
bash -l
$ bash -l
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash -il -or- bash -li
$ bash -il
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash -c "..cmd.."
$ bash -c 'echo hi'
hi
NOTE: Notice that the -c
switch didn't source either file!
disabling config files from being read
bash --norc
$ bash --norc
bash-4.1$
bash --noprofile
$ bash --noprofile
sourced /home/saml/.bashrc
bash --norc -i
$ bash --norc -i
bash-4.1$
bash --norc -l
$ bash --norc -l
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash --noprofile -i
$ bash --noprofile -i
sourced /home/saml/.bashrc
bash --noprofile -l
$ bash --noprofile -l
bash-4.1$
bash --norc -i -or- bash --norc -l
$ bash --norc -c 'echo hi'
hi
More esoteric ways to call bash
bash --rcfile $HOME/.bashrc
$ bash -rcfile ~/.bashrc
sourced /home/saml/.bashrc
bash --norc --rcfile $HOME/.bashrc
$ bash --norc -rcfile ~/.bashrc
bash-4.1$
These failed
bash -i -rcfile ~/.bashrc
$ bash -i -rcfile ~/.bashrc
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash: /home/saml/.bashrc: restricted: cannot specify `/' in command names
bash -i -rcfile .bashrc
$ bash -i -rcfile .bashrc
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash: .bashrc: command not found
There are probably more but you get the point, hopefully....
Lastly if you're so enthralled with this topic that you'd like to read/explore more on it, I highly suggest taking a look at the Bash Beginners Guide, specifically section: 1.2. Advantages of the Bourne Again SHell. The various subsections under that one, "1.2.2.1. Invocation" through "1.2.2.3.3. Interactive shell behavior" explain the low level differences between the various ways you can invoke bash
.
It is explained in Practical Unix & Internet Security as a security feature: uucp has to log in to issue commands, and uucico
is a limited-functionality shell, with a separate login/password from regular accounts.
Best Answer
You don't even need to use
:
; you can just> file
(at least inbash
; other shells may behave differently).In practical terms, there is no real difference here (though the minuscule overhead of calling out to
/bin/touch
is a thing).touch
, however, can also be used to modify the timestamps on a file that already exists without changing or erasing the contents; further,> file
will blow out anyfile
that already exists. This can be worked around by instead using>> file
.One other difference with
touch
is that you can have it create (or update the timestamp on) multiple files at once (e.g.touch foo bar baz quux
) with a more succinct syntax than with redirection, where each file needs its own redirection (e.g.>foo >bar >baz >quux
).Using
touch
:Using redirection: