How can I copy a variable to another variable in a shell script?
如何将变量复制到shell脚本中的另一个变量?
Assuming the user has passed in $1
, how can its value be copied to another variable?
假设用户通过了$1,它的值如何被复制到另一个变量?
I assume it would look something like this...
我想它应该是这样的……
cp $1 $2
echo "Copied value: $2"
5 个解决方案
#1
2
Note that cp
is used to copy files and directories
. To define variables, you just have to use the following syntax:
注意,cp用于复制文件和目录。要定义变量,只需使用以下语法:
v=$1
Example
$ cat a
echo "var v=$v"
v=$1
echo "var v=$v"
$ ./a 23 <---- we execute the script
var v= <---- the value is not set
var v=23 <---- the value is already set
#2
1
Firstly cp
is for copying files and directories only (as the man page states)
首先,cp仅用于复制文件和目录(如手册页所述)
Secondly, it is not possible to assign to an argument variable ($0..$1..$n). They are meant to be read only.
其次,不可能将参数变量赋值为$0. $1. $n。他们本应只读的。
You can do this instead:
你可以这样做:
input2=$1
It will copy the value of $1
to a new variable called $input2
它将把$1的值复制到名为$input2的新变量
#3
0
val=$1
echo "Copied Value : $val"
#4
0
You are using cp
which is for copying files.
您正在使用用于复制文件的cp。
Just use
只使用
v=$1
and echo it:
回声。
echo "Copied Variable: $v"
#5
0
i've found set -- to be a very useful command to set the positional parameters. e.g. in the example you gave, and well answered:
我已经找到了set——作为设置位置参数的非常有用的命令。在你举的例子中,回答得很好:
cp file1 file2
copies "file1" to "file2". frequently when i work with a few files, I'll do this instead:
副本file1 file2”。当我处理一些文件时,我通常会这样做:
set -- file1 file2
cp $1 $2
and if you want to reverse the names in the variables:
如果你想颠倒变量的名称:
set -- $2 $1 # puts the current "$2" value in "$1", and vice versa, then
cp $1 $2 # copies what was file2 contents back to file1.
this without using any "named" variables, which you've already seen. my more common use is like:
这将不使用您已经看到的任何“命名”变量。我更常用的用法是:
set -- ${1%.txt} # strips a ".txt" suffix
set -- $1 $1.out $1.err # sets 2nd to <whatever>.out and 3rd to <whatever>.err, so
cmd $1.txt > $2 2>$3 # puts stdout in ...out and stderr in ...err
v
v
#1
2
Note that cp
is used to copy files and directories
. To define variables, you just have to use the following syntax:
注意,cp用于复制文件和目录。要定义变量,只需使用以下语法:
v=$1
Example
$ cat a
echo "var v=$v"
v=$1
echo "var v=$v"
$ ./a 23 <---- we execute the script
var v= <---- the value is not set
var v=23 <---- the value is already set
#2
1
Firstly cp
is for copying files and directories only (as the man page states)
首先,cp仅用于复制文件和目录(如手册页所述)
Secondly, it is not possible to assign to an argument variable ($0..$1..$n). They are meant to be read only.
其次,不可能将参数变量赋值为$0. $1. $n。他们本应只读的。
You can do this instead:
你可以这样做:
input2=$1
It will copy the value of $1
to a new variable called $input2
它将把$1的值复制到名为$input2的新变量
#3
0
val=$1
echo "Copied Value : $val"
#4
0
You are using cp
which is for copying files.
您正在使用用于复制文件的cp。
Just use
只使用
v=$1
and echo it:
回声。
echo "Copied Variable: $v"
#5
0
i've found set -- to be a very useful command to set the positional parameters. e.g. in the example you gave, and well answered:
我已经找到了set——作为设置位置参数的非常有用的命令。在你举的例子中,回答得很好:
cp file1 file2
copies "file1" to "file2". frequently when i work with a few files, I'll do this instead:
副本file1 file2”。当我处理一些文件时,我通常会这样做:
set -- file1 file2
cp $1 $2
and if you want to reverse the names in the variables:
如果你想颠倒变量的名称:
set -- $2 $1 # puts the current "$2" value in "$1", and vice versa, then
cp $1 $2 # copies what was file2 contents back to file1.
this without using any "named" variables, which you've already seen. my more common use is like:
这将不使用您已经看到的任何“命名”变量。我更常用的用法是:
set -- ${1%.txt} # strips a ".txt" suffix
set -- $1 $1.out $1.err # sets 2nd to <whatever>.out and 3rd to <whatever>.err, so
cmd $1.txt > $2 2>$3 # puts stdout in ...out and stderr in ...err
v
v