0

I use some Linux commands more frequently like going to a specific directory, grep for some text etc. I assigned a variable for each command and kept them in a script file and I run the script file every time when I login to my Linux box so that I can use the variables instead of typing lengthy commands.

Is it possible that I can make sure that my script file runs everytime when I login to my Linux box so that I need not run it everytime?

Is there an alternate way of storing my frequently used commands so that they will be available when I open my Linux box?

Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • possible duplicate of [How do you run a script on login in \*nix?](http://stackoverflow.com/questions/97137/how-do-you-run-a-script-on-login-in-nix) – Andreas Fester Apr 03 '13 at 11:21
  • If the commands are frequently used, chances are those would be in the shell history. Press `Ctrl-R` and start typing out a few characters of the command you want to execute. This would bring up the last command including the specified character(s). Pressing `Ctrl-R` again brings up the next anterior command. – devnull Apr 03 '13 at 12:14

5 Answers5

2

If you are using bash (probably are), add it to your .bashrc. You will find it in your home directory.

Other shells have corresponding startup scripts.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
2

Adding commands to .bashrc for a non-login shell, or to .bash_profile for login shells (assuming, of course, that you're using bash).

From the bash manual entry:

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.

samlev
  • 5,852
  • 1
  • 26
  • 38
1

You have to put your script into your .bashrc file, it is in your home directory

nano ~/.bashrc

It only works when you are using bash.

Eun
  • 4,146
  • 5
  • 30
  • 51
0

What is about alias?

You can store them in the ~/.bashrc, when I am right.

fvosberg
  • 677
  • 10
  • 30
0

You can use a .bashrc file but this script is executed when you open an interactive Bash shell. That is every time you connect to a server with a terminal (if Bash is your default shell) or open another shell that opens an interactive shell (like su - $USER).

If you work locally with X-Window GUI on Linux (Unix) the script will be executed every time you open a terminal program (like Konsole in KDE or gnome-terminal). It maybe not what you expected. In this case you can hack a .xinit script or use your display manager or desktop environment way to execute a script upon start. It is hard to tell how because it is specific to your environment (Linux/ Unix distribution or desktop environment (KDE, GNOME, ...) ) .

digital_infinity
  • 534
  • 6
  • 20