防止 * 在 bash 脚本中被扩展 [英] Prevent * to be expanded in the bash script

查看:16
本文介绍了防止 * 在 bash 脚本中被扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux bash 脚本:

Linux bash script:

#!/bin/bash

function Print()
{
    echo $1
}

var="*"
Print $var

执行结果:

alex@alex-linux:~/tmp$ ./sample-script 
sample-script

* 扩展为文件列表,它实际上是脚本本身.如何防止这种情况并查看实际变量值?在一般情况下,var 可能比 * 更复杂,例如:home/alex/mydir/*.

* is expanded to the list of files, which is actually script itself. How can I prevent this and see actual variable value? In general case, var can be more complicated than *, for example: home/alex/mydir/*.

推荐答案

你也需要对变量进行转义:

you need to escape your variables, too:

Print "$var"

在你的函数中:

echo "$1"

这篇关于防止 * 在 bash 脚本中被扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆