In the previous posts we talked about which files you could use to customize your bash environment.
Note: bash aliases are something completely different from Finder aliases. The closest shell equivalent for Finder alias files are symbolic links.
Configuring bash with aliases and functions
For example a common alias is to define:. You can type this alias definition directly into bash but then the definition will only exist for that particular shell. When you open a new terminal window it will know nothing of this alias. If you want an alias to exist in all your shells and usually you do , you need to add the above line to your.
Learn about the difference here. Whenever you modify the.
Once you have set the alias, anytime you type ll at the start of a command, bash will replace it with ls -l before executing. Since subsequent arguments are left alone they will just be picked up by the substituted ls command, so if you type ll -a bash will substitute that to ls -l -a and it will work just your would expect.
Bash Shell Scripting
If you always want to use the long format of ls , you can alias the ls command itself:. Then, when ever you type ls it will be replaced with ls -l. Since alias substitution only happens at the beginning of the command, you can also bypass it once by starting the command line with a different character:.
Either way of typing this will use the original ls command, avoiding substitution, just this once. If you are unsure if a command has an alias or want to know what gets substituted, then you can have the alias definition printed with.
Some users like to alias the potentially dangerous commands rm , mv and cp with the -i option, which forces the user to confirm when a file is going to be deleted or overwritten:.
Since alias substitution only takes place at the beginning of the command, you can still use.. Note that the last alias cd.. Since the output of the alias is not checked for further alias substitutions you cannot use alias cd..
Each alias must stand for itself. This is useful to get notified of long running commands:. The dockspace alias uses this hack to add a spacer item to the dock.
We have seen earlier with the ll alias that any text or arguments after the alias will just be added to the substituted command. We can use this to our advantage:.
While bash aliases are useful and easy to define, they have limitations. Aliases will only be replaced at the beginning of the command prompt. Also additional arguments will be appended after the replacement. If you need to insert additional arguments somewhere in the replacement, you cannot achieve that with aliases.
For example, to display the man page for ls in the Preview application you need the following commands:.
If you wanted to alias this you would have to insert the arguments after man -t and before the pipe. Since aliases, only substitute the beginning of the command, this will not work. Sometimes you will see bash functions defined without the function keyword, but I prefer to use it since it makes the code more readable. Within functions the full power of bash scripting is at your service.
You can even call other scripting languages.
The last alias cdf has to be defined as an alias. Since a function or script could not change the directory for the current shell, you have to use alias substitution to get the cd.
Call bashrc function from script to book
In general functions and scripts are more powerful and versatile than aliases, but aliases provide and easy and comparatively safe way to customize the your shell environment. Here is a sample. You can use this as a starting point for your own bash configuration. Your email address will not be published.
Notify me of follow-up comments by email. Notify me of new posts by email.
Bash Scripting Part6 – Create and Use Bash Functions
This site uses Akismet to reduce spam. Learn how your comment data is processed. Skip to content.
Shell Scripting - Functions
There are mainly three customizations you can perform: environment variables aliases functions This post will look at some useful aliases and functions. You can add short cuts to cd to parent folders: alias.. Functions: beyond the alias While bash aliases are useful and easy to define, they have limitations.
Subscribe to RSS
Putting it all together Here is a sample. View the code on Gist. Leave a Reply Cancel reply Your email address will not be published.