任意のコマンドの’set alias’とaliasコマンドは対話型シェルで正常に動作しますが、エイリアシングはスクリプト内では機能しません。

1. インタラクティブシェル

# alias ls1='ls -lrt'# ls1total 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. スクリプト内

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

エイリアスは、shoptを使用してexpand_aliasesシェルオプションが設定されていない限り、シェルが対話, これは、単純なbashスクリプトにコマンド”alias”を追加することによってテストすることができ、スクリプトの実行はaliasコマンドを与えませんが、対話型シェ

Bashのmanページから:

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).

bashスクリプトでエイリアスを動作させる

bashスクリプトでエイリアスコマンドを動作させるには、次のアプローチを使用できます。, Bashスクリプトで変数を使用して任意のコマンドの優先オプションを設定でき、これらの変数はスクリプトの後のセクションで参照して、スクリプト内のエイリアスが必要であれば十分です。

スクリプトの先頭に’shopt-s expand_aliases’コマンドを追加して、エイリアスを展開し、bashスクリプトでエイリアスコマンドを動作させます。

# cat script.sh#!/bin/bash# Script to check the alias outputshopt -s expand_aliasesalias ls1='ls -lrt'ls1
# chmod +x script.sh# ./script.shtotal 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