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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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 mount and umount a file system in Linux
  2. UNIX / Linux : How to lock or disable an user account
  3. How to disable avahi-daemon service in CentOS/RHEL
  4. CentOS / RHEL 7 : sysctl kernel parameter doesn’t take effect after reboot
  5. What are SELinux Users and how to Map Linux Users to SELinux Users
  6. How to Replace a Failed Btrfs Device
  7. Difference between the Java heap and native C heap
  8. How to use strace and ltrace commands in Linux
  9. CentOS / RHEL 6 : how to start the services interactively during boot (to disable/abort some services)
  10. Installing CentOS / RHEL 7 (step by step with screen shots)

You May Also Like

Primary Sidebar

Recent Posts

  • How to disable ACPI in CentOS/RHEL 7
  • How to Use real-time query to access data on a physical standby database
  • CentOS/RHEL 8: “ACPI MEMORY OR I/O RESET_REG” Server Hung after reboot
  • How to Create a Physical Standby Database by Using SQL and RMAN Commands
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary