简体   繁体   English

如何在鱼壳中设置变量?

[英]How to set variables in Fish shell?

The set command in my fish shell in Ubuntu (elementary OS and Linux Mint) doesn't work.我在 Ubuntu(基本操作系统和 Linux Mint)中的鱼壳中的 set 命令不起作用。 The variables stay empty and even the examples in the tutorials don't work, ie:变量保持为空,甚至教程中的示例也不起作用,即:

(set foo hi --> # Sets the value of the variable $foo to be 'hi'.). (set foo hi --> # 将变量 $foo 的值设置为 'hi'。)。

In the script I am trying to do the following,在脚本中,我尝试执行以下操作,

set COUNTRY US CN MX

but when I want to call the variable by $COUNTRY there is no answer.但是当我想通过$COUNTRY调用变量时,没有答案。

What am I doing wrong?我究竟做错了什么?

Are you really using the fish shell?你真的用fish的壳呢? Because your example works for me:因为你的例子对我有用:

$ set COUNTRY US CN MX
$ echo $COUNTRY
US CN MX
$ set --show COUNTRY
$COUNTRY: not set in local scope
$COUNTRY: set in global scope, unexported, with 3 elements
$COUNTRY[1]: length=2 value=|US|
$COUNTRY[2]: length=2 value=|CN|
$COUNTRY[3]: length=2 value=|MX|
$COUNTRY: not set in universal scope

I suspect you are trying to use COUNTRY in another process.我怀疑您正试图在另一个进程中使用COUNTRY In which case you need to export the var using set -x COUNTRY US CN MX .在这种情况下,您需要使用set -x COUNTRY US CN MX导出 var。 But note that fish vars are arrays and exporting a var with more than one value won't be intelligible to a child process (unless that child process is a fish shell).但请注意,fish vars 是数组,并且导出具有多个值的 var 将无法被子进程理解(除非该子进程是一个fish shell)。

Also, it sounds like you might be trying to modify a variable in a parent process via a fish script.此外,听起来您可能正在尝试通过鱼脚本修改父进程中的变量。 That won't work.那行不通。 You cannot modify the variables of a parent process.您不能修改父进程的变量。 Not even if they are an environment variable.即使它们是环境变量,也不会。 This is not a fish limitation.这不是鱼的限制。 It is inherent in the design of the UNIX process model.它是 UNIX 进程模型设计中固有的。

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

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