xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to print a string with a variable by using the echo command in the shell script All In One

How to print a string with a variable by using the echo command in the shell script All In One

Node.js & nvm

question

I defined a shell variable in the terminal and then used the echo command to output a string with that variable and it worked fine in the terminal.

But when I use the same command in a shell script, the variable disappears without any output or error.

So, what's going on.

terminal

$ NVM_USER=xgqfrms

$ echo $NVM_USER
xgqfrms

$ echo "string with a variable $NVM_USER"
string with a variable xgqfrms
$ echo string with a variable $NVM_USER
string with a variable xgqfrms
$ echo "string with a variable ${NVM_USER}"
string with a variable xgqfrms

enter image description here

shell script

#!/usr/bin/env bash

echo $NVM_USER
echo "string with a variable $NVM_USER"
echo string with a variable $NVM_USER
echo "string with a variable ${NVM_USER}"

enter image description here

I've searched and tried some solutions but still stuck.

update

It works using a variable defined in shell scripts.

#!/usr/bin/env bash

user=xgqfrms
echo "string with a variable ${user}"

# temp=$(echo $NVM_USER)
echo "string with a variable ${temp}"

enter image description here

solutions

1. when using the same terminal (only one shell)

use export to change a shell variable to a environment variable;

# terminal
$ export NVM_USER=xgqfrms
$ ./test.sh

enter image description here

As answered by Gilles Quénot

2. when using two different terminals (two shells / child processes)

bash demo

to make an environment variable persistent by defining a variable in the bash configuration file.

# terminal 1

# 1. add your export variable to the shell config file
$ sudo vim .bashrc
export NVM_USER=xgqfrms

# fresh the config
$ source ~/.bashrc

# terminal 2
$ ./test.sh

image

zsh demo

# terminal 1

# 1. add your export variable to the shell config file
$ sudo vim .zshrc
export NVM_USER=xgqfrms

# fresh the config
$ source ~/.zshrc

enter image description here

environment variable vs shell variable

Variables can be classified into two main categories, environment variables, and shell variables.

Environment variables are variables that are available system-wide and are inherited by all spawned child processes and shells.

Shell variables are variables that apply only to the current shell instance. Each shell such as zsh and bash, has its own set of internal shell variables.

There are several commands available that allow you to list and set environment variables in Linux:

  • env – The command allows you to run another program in a custom environment without modifying the current one. When used without an argument it will print a list of the current environment variables.
  • printenv – The command prints all or the specified environment variables.
  • set – The command sets or unsets shell variables. When used without an argument it will print a list of all variables including environment and shell variables, and shell functions.
  • unset – The command deletes shell and environment variables.
  • export – The command sets environment variables.

demos

  1. /etc/environment – This file is used to set up system-wide environment variables.
# 对所有登录的用户都会生效,因为修改的是所有登录用户的 env 配置文件
$ sudo vim /etc/environment
NVM_USER_ENV=xgqfrms

$ ./shell-env-variable-test.sh
  1. export /home/$USER – This file is used to set up environment variables for current logined user when login.
# 仅对当前的登录的用户生效,因为修改的是当前登录用户的启动配置文件
$ pwd
# /home/eric
$ cd ~
$ sudo vim .bashrc
export NVM_USER_ENV=xgqfrms

# 更新
$ source .bashrc

$ ./shell-env-variable-test.sh
  1. shell variable
# 临时 shell variable, 仅对创建它的 shell 有效
$ NVM_USER_ENV=xgqfrms ./shell-env-variable-test.sh

点击查看代码详情

eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER=xgqfrms
eric@rpi4b:~ $ set | grep NVM_USER
NVM_USER=xgqfrms
NVM_USER_ENV=xgqfrms
NVM_USER_shell_var=2023
eric@rpi4b:~ $ NVM_USER_ENV=rpi4b ./shell-env-variable-test.sh
rpi4b
string with a variable rpi4b
string with a variable rpi4b
string with a variable rpi4b
eric@rpi4b:~ $ set | grep NVM_USER
NVM_USER=xgqfrms
NVM_USER_ENV=xgqfrms
NVM_USER_shell_var=2023
eric@rpi4b:~ $ ./shell-env-variable-test.sh

string with a variable 
string with a variable
string with a variable 
eric@rpi4b:~ $ 

image

eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER=xgqfrms
eric@rpi4b:~ $ set | grep NVM_USER
NVM_USER=xgqfrms
NVM_USER_ENV=xgqfrms
NVM_USER_shell_var=2023
eric@rpi4b:~ $ cat ./test.sh
#!/usr/bin/env bash

echo $NVM_USER
echo "string with a variable $NVM_USER"
echo string with a variable $NVM_USER
echo "string with a variable ${NVM_USER}"
eric@rpi4b:~ $ ./test.sh
xgqfrms
string with a variable xgqfrms
string with a variable xgqfrms
string with a variable xgqfrms
eric@rpi4b:~ $ cat ./shell-env-variable-test.sh
#!/usr/bin/env bash

echo $NVM_USER_ENV
echo "string with a variable $NVM_USER_ENV"
echo string with a variable $NVM_USER_ENV
echo "string with a variable ${NVM_USER_ENV}"
eric@rpi4b:~ $ ./shell-env-variable-test.sh

string with a variable 
string with a variable
string with a variable 
eric@rpi4b:~ $ echo 当前父 shell 的 shell 变量 $NVM_USER_ENV 对子 shell 不可见
# 当前父 shell 的 shell 变量 xgqfrms 对子 shell 不可见

shell-env-variable-test.sh

#!/usr/bin/env bash

echo $NVM_USER_ENV
echo "string with a variable $NVM_USER_ENV"
echo string with a variable $NVM_USER_ENV
echo "string with a variable ${NVM_USER_ENV}"

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

parent shell vs child shell

打开 shell 的三种方式

  1. terminal 登录后的交互式 shell (父 shell)
  2. 输入 bash 打开一新的交互式 shell (子 shell)
  3. 运行 shell script 创建的非交互式 shell (子 shell)

global environment variables vs local environment variables / shell variables

全局环境变量, 父 shell、子 shell 都可以访问具有共享性; 虽然可以使用同名变量对其进行临时覆盖、临时删除,但是操作的影响只会对当前 shell 有效,不会向上传递,即不会真的修改或删除全局变量;

局部环境变量, 也叫 shell variable,只会对当前定义它的 shell 生效,其父 shell 或 子 shell 都无法访问该局部环境变量,即具有私有性,不可共享;

eric@rpi4b:~ $ printenv NVM_USER_ENV
eric@rpi4b:~ $ printenv | grep NVM_USER_ENV
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ printenv NVM_USER
xgqfrms_pi4b
eric@rpi4b:~ $ env NVM_USER
env: “NVM_USER”: 没有那个文件或目录
eric@rpi4b:~ $ export NVM_USER_ENV=eric_env
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ export NVM_USER_ENV=eric_env_new
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ bash
fix vim bug ✅
eric@rpi4b:~ $ echo $NVM_USER
xgqfrms_pi4b
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ echo $NVM_USER_ENV
eric_env_new
eric@rpi4b:~ $ export NVM_USER_ENV=eric_env_child
eric@rpi4b:~ $ echo $NVM_USER_ENV
eric_env_child
eric@rpi4b:~ $ exit
exit
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ 
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b

eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ bash
fix vim bug ✅
eric@rpi4b:~ $ echo $NVM_USER_ENV
eric_env_new
eric@rpi4b:~ $ export NVM_USER_ENV_CHILD=child_shell_env
eric@rpi4b:~ $ echo $NVM_USER_CHILD

eric@rpi4b:~ $ echo $NVM_USER_ENV_CHILD
child_shell_env
eric@rpi4b:~ $ exit
exit
eric@rpi4b:~ $ echo $NVM_USER_ENV_CHILD

eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ 



nvm PR

new

  nvm_echo "=> Close and reopen your terminal to start using nvm or run the command \`source ${NVM_PROFILE}\` or run the following to use it now:"

image

https://github.com/nvm-sh/nvm/pull/3201

old

image

refs

https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/#disqus_thread

https://www.freecodecamp.org/news/how-to-set-an-environment-variable-in-linux/

https://earthly.dev/blog/bash-string/



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-09-21 13:57  xgqfrms  阅读(6)  评论(4编辑  收藏  举报