creating an alias in ubuntu , in .profile
What happens if you log out and back in?
Alternatively you could just type
source .profile
and that should activate your alias. Many people using bash
set their aliases up in the .bashrc
file.
Creating permanent executable aliases
Add the command to your ~/.bashrc
file.
To make it available to all users, add it to /etc/profile
.
How do I create a Bash alias?
You can add an alias
or a function
in your startup script file. Usually this is .bashrc
, .bash_login
or .profile
file in your home directory.
Since these files are hidden you will have to do an ls -a
to list them. If you don't have one you can create one.
If I remember correctly, when I had bought my Mac, the .bash_login
file wasn't there. I had to create it for myself so that I could put prompt info
, alias
, functions
, etc. in it.
Here are the steps if you would like to create one:
- Start up Terminal
- Type
cd ~/
to go to your home folder - Type
touch .bash_profile
to create your new file. - Edit
.bash_profile
with your favorite editor (or you can just typeopen -e .bash_profile
to open it in TextEdit. - Type
. .bash_profile
to reload.bash_profile
and update any alias you add.
How to make an alias for a long path?
Since it's an environment variable (alias has a different definition in bash
), you need to evaluate it with something like:
cd "${myFold}"
or:
cp "${myFold}/someFile" /somewhere/else
But I actually find it easier, if you just want the ease of switching into that directory, to create a real alias (in one of the bash
startup files like .bashrc
), so I can save keystrokes:
alias myfold='cd ~/Files/Scripts/Main'
Then you can just use (without the cd
):
myfold
To get rid of the definition, you use unalias
. The following transcript shows all of these in action:
pax> cd ; pwd ; ls -ald footy
/home/pax
drwxr-xr-x 2 pax pax 4096 Jul 28 11:00 footy
pax> footydir=/home/pax/footy ; cd "$footydir" ; pwd
/home/pax/footy
pax> cd ; pwd
/home/pax
pax> alias footy='cd /home/pax/footy' ; footy ; pwd
/home/pax/footy
pax> unalias footy ; footy
bash: footy: command not found
Make a Bash alias that takes a parameter?
Bash alias does not directly accept parameters. You will have to create a function.
alias
does not accept parameters but a function can be called just like an alias. For example:
myfunction() {
#do things with parameters like $1 such as
mv "$1" "$1.bak"
cp "$2" "$1"
}
myfunction old.conf new.conf #calls `myfunction`
By the way, Bash functions defined in your .bashrc
and other files are available as commands within your shell. So for instance you can call the earlier function like this
$ myfunction original.conf my.conf
run a function or alias set in bashrc, or profile through nohup
nohup
will not work with functions. You need to create a shell script which wraps and executes the function. The shell script then you can run with nohup
Like this:
test.sh
#!/bin/bash
function hello_world {
echo "hello $1, $2"
}
# call function
hello_world "$1" "$2"
chmod +x test.sh
and then call it in your for
loop:
for i in `cat mylist`; do
nohup ./test.sh $i 'mycommand' &
done
Related Topics
Write Something to Linux Hid Device
How to Compile Redis So That I Can Upload and Run It on Shared Hosting
How to Type "Cargo Run" Without Needing to Set The Ld_Library_Path Shell Variable
Different Behavior of Which Command in Zsh and Bash
Mpc/Mpd on Linux: How to Play Local Wav File
How to Remove File with Special Characters
Bypassing 4Kb Block Size Limitation on Block Layer/Device
How to Handle Error/Exception in Shell Script
Finding Directories with Find in Bash Using a Exclude List
Convert Object File to Another Architecture
How to Include a Directory in The Package Debuild
Qt Does Not Create Output Files in Debug/Release Folders in Linux
How to Set Environment Variable in Linux Permanently
Count The Occurrence of a String in an Input File
Hbase Does Not Run After ./Start-Hbase.Sh - Permission Denied