在 shell 脚本中使用 bash $HOME
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14561475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use bash $HOME in shell script
提问by idobr
How to made bash to execute variable value. For example, we have this code, where variable value was set in single quotes(!).
如何使 bash 执行变量值。例如,我们有这样的代码,其中变量值设置在单引号(!)中。
#!/bin/bash
V_MY_PATH='$HOME'
echo "$V_MY_PATH"
ls $V_MY_PATH
The output is
输出是
$HOME
ls: $HOME: No such file or directory
How to made bash to translate shell variable insto its value if there is some.
如果有的话,如何让 bash 将 shell 变量转换为其值。
I want to add some code after V_MY_PATH='$HOME' to make output like echo $HOME.
我想在 V_MY_PATH='$HOME' 之后添加一些代码来制作类似 echo $HOME 的输出。
It's something simple, but i'm stuck. (NB: I know that with V_MY_PATH="$HOME", it works fine.)
这很简单,但我被卡住了。(注意:我知道使用 V_MY_PATH="$HOME",它工作正常。)
EDIT PART: I just wanted to make it simple, but I feel that some details are needed.
编辑部分:我只是想让它变得简单,但我觉得需要一些细节。
I'm getting parameter from a file. This part works good. I don't want to rewite it. The problem is that when my V_MY_PATH contains a predefined variable (like $home) it's not treated like its value.
我正在从文件中获取参数。这部分效果很好。我不想重写它。问题是当我的 V_MY_PATH 包含一个预定义的变量(如 $home)时,它不会被视为它的值。
回答by user000001
Remove the single quotes
去掉单引号
V_MY_PATH='$HOME'
should be
应该
V_MY_PATH=$HOME
you want to use $HOME
as a variable
你想$HOME
用作变量
you can't have variables in single quotes.
单引号中不能有变量。
Complete script:
完整脚本:
#!/bin/bash
V_MY_PATH=$HOME
echo "$V_MY_PATH"
ls "$V_MY_PATH" #; Add double quotes here in case you get weird filenames
Output:
输出:
/home/myuser
0
05430142.pdf
4
aiSearchFramework-7_10_2007-v0.1.rar
etc.
等等。
回答by peteches
use variable indirect reference so:
使用变量间接引用,所以:
pete.mccabe@Hymanfrog$ p='HOME'
pete.mccabe@Hymanfrog$ echo $p
HOME
pete.mccabe@Hymanfrog$ ls ${p}
ls: cannot access HOME: No such file or directory
pete.mccabe@Hymanfrog$ ls ${!p}
bash libpng-1.2.44-1.el6 python-hwdata squid
...
pete.mccabe@Hymanfrog$
The ${!p} means take the value of $p and that value is the name of the variable who's contents I wish to reference
${!p} 表示取 $p 的值,该值是我希望引用的内容的变量的名称
回答by hwac121
You can use single or double quotes, and in your case, none if you prefer.
You have nothing telling bash what the variable is equal to. Maybe something like this? (unless I misunderstand what you are trying to do)
您可以使用单引号或双引号,在您的情况下,如果您愿意,可以不使用。
您无法告诉 bash 变量等于什么。也许是这样的?(除非我误解了你要做什么)
======================================================================
================================================== ====================
#!/bin/bash
#########################
# VARIABLES #
#########################
V_MY_PATH=home
#########################
# SCRIPT #
#########################
echo "What is your home path?"
read $home
echo "Your home path is $V_MY_PATH"
ls $V_MY_PATH
Of course you could also just remove the variable at the top and use: echo "Your home path is $home"
当然,您也可以删除顶部的变量并使用:echo "Your home path is $home"