• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to make alias command work in bash script or bashrc file

by admin

‘set alias‘ for any command and the alias command will work fine on the interactive shell, whereas aliasing doesn’t work inside the script.

1. Interactive shell

# alias ls1='ls -lrt'
# ls1
total 0
-rw-r--r-- 1 root root 0 Oct 12 12:14 file1
-rw-r--r-- 1 root root 0 Oct 12 12:14 file2

2. Inside the script

# cat script.sh
#!/bin/bash
# Script to check the alias output
alias ls1='ls -lrt'
ls1
# chmod +x script.sh
# ./script.sh 
./script.sh: line 3: ls1: command not found

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt. It can be tested by adding the command “alias” to simple bash script and the script execution will not give the alias command, whereas on the interactive shell it will provide the available list of aliasing as shown in the above example.

From the man page of Bash :

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).

Making alias work in bash script

The following approach can be used, for making alias command work in bash scripts. Variables can be used in the bash script to set the preferred options for any command and those variables can be referred in the later section of script to suffice the need of alias inside scripts.

Add the command ‘shopt -s expand_aliases’ at the start of the script to expand aliases and make alias command work in the bash script.

# cat script.sh
#!/bin/bash
# Script to check the alias output
shopt -s expand_aliases
alias ls1='ls -lrt'
ls1
# chmod +x script.sh
# ./script.sh
total 0
-rw-r--r-- 1 root root 0 Oct 12 12:14 file1
-rw-r--r-- 1 root root 0 Oct 12 12:14 file2

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to set children-max for udev Service in CentOS/RHEL 7
  2. pvdisplay Command Examples in Linux
  3. How to Partition DM-Multipath Pseudo Devices in CentOS/RHEL
  4. CentOS / RHEL : Installing and Configuring ASMLib
  5. How To Add New Disk to An Existing Diskgroup on RAC Cluster or Standalone ASM Configuration
  6. Linux Boot Process
  7. PAM password complexity and pam_cracklib credit system in CentOS/RHEL
  8. lvresize command examples in Linux
  9. How to Configure Existing Lvm Volume Group to Use DM-Multipath
  10. How to Find and Delete Empty Directories and Files in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • vgextend Command Examples in Linux
  • setpci command – configure PCI device
  • db_load command – generate db database
  • bsdtar command – Read and write tape archive files

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright