Shell aliases

Is there any way to incorporate sh aliases in bash, or is it possible to use something like a -include in both rc's if there are aliases that are common?
 
[…] if there are aliases that are common?
I only use aliases for shell built-ins. For anything else I use shell scripts. It’s cleaner, available in any shell I use, and suitable for elaborate logic (e. g. you can refuse harmful arguments). For example I want an “alias” for stat. My shell script is:​
Bash:
#!/bin/sh
[ -t 0 ] || exec /usr/bin/stat "${@}"

exec /usr/bin/stat -f \
'                     file: %N
                     kind: %HT
                     mode: %Sp
                    owner: %5u “%Su”
              group owner: %5g “%Sg”
            serial number: %-20i
        generation number: %-20v
          number of links: %l
        major device type: 0x%#Hr
        minor device type: 0x%#Lr
            device number: %d
             logical size: %14z bytes
               birth time: %SB (@%B)
         last access time: %Sa (@%a)
   data modification time: %Sm (@%m)
 status modification time: %Sc (@%c)
                  payload: %14b blocks
optimal I/O transfer size: %14k
       user defined flags: %#Xf' -t '%Y‑%m‑%d %H∶%M∶%S %Z' "${@}"
This file is stored at /usr/local/bin/stat, or better in a separate directory so they can be enabled/disabled simply by modifying $PATH (and possibly hash ‑r). For user-specific aliases I have a bin directory in the user’s home and set PATH=${HOME}/bin:${PATH}.​
 
Aliases in bash are easy to use, and documented. The bash man page (which SirDice pointed at) is a starting point; personally I don't like reading it, because it is very long and not well structured. If you search the web, there are lots of bash tutorials, which go over things in chapters.

I actually disagree to some extent with Kai's advice of moving the function of an alias into a script. He is right if the script is complex, for example many dozens of lines. But a script has significant overhead: It needs to be installed (in a bin directory, usually /usr/local/...), and it means another shell invocation with the CPU and IO overhead. For small things (1 to a dozen lines), I actually prefer shell functions, which are defined in a rc file. Here's an example: A command that creates a directory and immediately cd's into it:
Code:
   function mkdircd () {
        if [ $# -ne 1 ] ; then
            echo "Usage: mkdircd <dir>"
            echo "       Create a directory and cd into it"
            return ; fi

        mkdir -p $1 || return
        cd $1
    }
Storing several such small things in .bashrc or .shrc is more convenient than creating a standalong script for each of them.

Finally, I think balanga's original question wasn't just whether and how to use aliases in bash, but also how to share the alias definition when migrating from sh to bash. And for simple aliases, that's easy, because there both sh and bash use the same syntax, for example alias ll='ls lF'. One could for example place all alias definitions that are to be shared in a separate file (call it for example .aliasrc), and then source the file from both .bashrc and .shrc (using the source or . command, not by executing it).
 
Back
Top