别名在 bash shell 脚本中不起作用

Bor*_* Me 5 bash scripts alias 14.04

当我在系统中搜索 .bashrc 文件时,我得到:

/etc/bash.bashrc
/etc/skel/.bashrc
/root/.bashrc
/usr/share/base-files/dot.bashrc
Run Code Online (Sandbox Code Playgroud)

我变了:

/etc/bash.bashrc   
/root/.bashrc
Run Code Online (Sandbox Code Playgroud)

我添加了别名。

alias urldecode='python -c "import sys, urllib as ul; \
    print ul.unquote_plus(sys.argv[1])"'

alias urlencode='python -c "import sys, urllib as ul; \
    print ul.quote_plus(sys.argv[1])"'
Run Code Online (Sandbox Code Playgroud)

当我运行命令时:

urlencode 'http://example.com space'
Run Code Online (Sandbox Code Playgroud)

它在命令行中工作正常,但是当我创建一个.sh文件并将相同的命令放在那里时,我得到:

./tf.sh: line 19: urlencode: command not found
Run Code Online (Sandbox Code Playgroud)

怎么了?

文件内容tf.sh

IP=$(curl -d "tool=checurl"-X POST https://site.com/get.py)
url='https://site.com.com/'
path=$(grep -oP '(?<=get.py\?filetype=csv\&data=).*?(?=\")' <<< $IP)
pathfull="get.py?filetype=csv&data=$path"

full=$url$pathfull

#echo $full

urlencode '$full'
Run Code Online (Sandbox Code Playgroud)

Yar*_*ron 10

一些评论:

  1. 编写 bash shell 脚本时,您应该使用以下命令启动脚本:

    #!/bin/bash
    
    Run Code Online (Sandbox Code Playgroud)
  2. 有一个已知问题 - 为什么我的 Bash 脚本无法识别别名?- bash 脚本无法识别别名。

解决该问题的一种选择是:

在脚本的开头(在 后面#!/bin/bash)添加:

shopt -s expand_aliases
Run Code Online (Sandbox Code Playgroud)

后面是source带有别名的文件:

source /etc/bash.bashrc
Run Code Online (Sandbox Code Playgroud)