linux - #!/bin/bash 可执行文件带有参数问题

标签 linux bash parameters

为什么我无法理解这里的问题?参数 catch 在/bin 之外工作,但我不想使用“./”来运行我计划执行的命令...

我将此脚本添加到 /bin 目录中:

$ cat /bin/test
#!/bin/bash
echo $@

当我执行它时,我收到一个错误。例如:

$ test one two
bash: test: one: unary operator expected

但是,当我将相同的脚本放在不同的位置时:

$ cat /home/user/run/test
#!/bin/bash
echo $@

运行正确:

$ /home/user/run/test one two
one two

最佳答案

解决方案是不要将 shell 内置命令或系统命令的名称用于您的个人脚本。

这是您观察到的问题:

$ test one two 
bash: test: one: unary operator expected

但是,请注意:

$ type test
test is a shell builtin

由于 test 是 shell 内置命令,因此您的命令将被忽略并运行 shell 内置命令。

为您的 shell 脚本选择另一个名称,最好是不与标准系统命令冲突的名称。

关于linux - #!/bin/bash 可执行文件带有参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48654827/

相关文章:

linux - 如果使用了设备 block 大小,mbuffer 比 dd 慢

c - 如何在 Linux 中使用自旋锁?

linux - 无法在 x86_32 中编译(错误 _ __asm__ 中的不可能约束)

c - pthread_mutex 和进程终止

linux - LD_LIBRARY_PATH 副作用

javascript - 如何保存函数调用之间的参数值?

c++ - getopt加入参数

mysql - 在 bash 中将标准输出转换为 utf8

linux - 解压缩连接两个 gzip 文件的结果是否总是等于以原始和解压缩形式连接两个文件的结果?

java - 在 Application.launch(Class, String... args) 中获取参数