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
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}"
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}"
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
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
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
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
/etc/environment
– This file is used to set upsystem-wide
environment variables.
# 对所有登录的用户都会生效,因为修改的是所有登录用户的 env 配置文件
$ sudo vim /etc/environment
NVM_USER_ENV=xgqfrms
$ ./shell-env-variable-test.sh
- 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
- 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:~ $
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}"
parent shell
vs child shell
打开 shell 的三种方式
- terminal
登录
后的交互式 shell (父 shell) - 输入
bash
打开一新的交互式 shell (子 shell) - 运行
shell script
创建的非交互式 shell (子 shell)
global
environment variables vslocal
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:"
https://github.com/nvm-sh/nvm/pull/3201
old
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-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17719773.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-09-21 The ways to express gratitude in English All In One
2022-09-21 Node.js 面试题 All In One
2022-09-21 js generate ASCII table dict All In One
2021-09-21 vue $nextTick All In One
2020-09-21 styled-components all in one
2020-09-21 CORS All In One
2019-09-21 js & bitwise operator