简体   繁体   English

如何在Shell函数中将整数作为参数传递并检索它

[英]How to pass an integer as an argument in Shell function and retrieve it

I have a small function like this - 我有一个这样的小功能-

#! /bin/sh
function hey() {
    a=$1
    echo $a
}

x=2
hey x

As per my understanding it should print 2 but it is printing x . 根据我的理解,它应该打印2但它正在打印x How to resolve it? 怎么解决呢?

Running you code in my bash (not sh) , it also prints x. 用我的bash(不是sh)运行代码,它也会打印x。 But if i change the last line to 但是如果我将最后一行更改为

hey $x 

then it prints 2. 然后打印2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM