首页 > 系统相关 >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

时间:2023-09-21 14:13:16浏览次数:33  
标签:shell string script echo USER xgqfrms variable NVM

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

# terminal
$ export NVM_USER=xgqfrms

$ ./test.sh

enter image description here

As answered by Gilles Quénot

2. when using different terminals

bash demo

# 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

demos

(

标签:shell,string,script,echo,USER,xgqfrms,variable,NVM
From: https://www.cnblogs.com/xgqfrms/p/17719773.html

相关文章

  • 无涯教程-JavaScript - COUNTIFS函数
    描述COUNTIFS函数将多个条件应用于跨多个范围的单元格,并计算满足所有条件的次数。语法COUNTIFS(criteria_range1,criteria1,[criteria_range2,criteria2]…)争论Argument描述Required/Optionalcriteria_range1Thefirstrangeinwhichtoevaluatetheassociat......
  • Typescript 测试驱动开发 TDD (8)
    强制运行和跳过测试 (Forcingandskippingtests)在处理一个已经编写了多个测试的应用程序时,我们通常只想运行特定的测试或者一组特定的测试。这种操作被称为强制运行测试,因为我们要求整个测试套件只运行指定的测试。可以通过以下两种方式来实现:1describe("agroupoftest......
  • Typescript 测试驱动开发 TDD (7)
    分组测试(Groupingtests)在一个测试规范文件中,我们可能希望将我们的测试分组为逻辑集合。Jest使用describe函数来实现这个目的,如下所示的测试代码:1describe("agroupoftests",()=>{2test("firsttest",()=>{3expect("stringvalue").toEqual("......
  • Typescript 测试驱动开发 TDD (6)
    观察模式(Watchmode)Jest和其他测试框架一样,也可以在观察模式下运行。这意味着它会监视项目中的所有文件,并在任何文件更改时自动重新运行任何测试。让我们按照以下方式更新我们的package.json文件:{..."scripts":{"test":"jest--watchAll--verbose"......
  • 记录 umi4 ant design pro typescript 在 vscode 代码提示错误的问题
    原因是vscode使用的ts版本与项目不匹配。修复方法:在vscode拓展【左侧4个方框的图标】搜索typescript下载安装插件JavaScriptandTypeScriptNightly然后使用ctrl+shift+p调出命令,使用SelectTypeScriptversion命令选择项目应用的typescript版本。选择使用工作区版......
  • Typescript 测试驱动开发 TDD (5)
    ts-jestJest是一个JavaScript测试框架,因此它会在我们的项目中寻找JavaScript测试来运行。我们可以运行TypeScript编译器生成JavaScript文件,或者使用像ts-jest这样的框架。ts-jest是一个TypeScript到Jest的桥接工具,它将负责编译步骤和与Jest的集成。事实上,ts-je......
  • Typescript 测试驱动开发 TDD (4)
    JestJest是一个简单易配置且功能强大的JavaScript单元测试框架,它构建在流行的Jasmine框架之上。Jasmine已经存在很长时间了,是一个成熟、功能齐全且广泛使用的测试框架。Jest通过使配置更加容易,并提供丰富的额外功能来增强Jasmine。Jest还可以并发运行测试,这显著加快了测试套件运......
  • Javascript中window.setInterval和window.setTimeout的区别
    在使用JScript的时候,我们有时需要间隔的执行一个方法,比如用来产生网页UI动画特效啥的。这是我们常常会使用方法setInterval或setTimeout,但是由于这两个方法是由脚本宿主模拟出来的Timer线程,在通过其调用我们的方法是不能为其传递参数。我们常用的使用场景是:代码如下:window.setTi......
  • 无涯教程-JavaScript - COUNTIF函数
    描述COUNTIF函数计算符合条件的单元格数。语法COUNTIF(range,criteria)争论Argument描述Required/Optionalrange您要计数的单元格组。范围可以包含数字,数组,命名范围或包含数字的引用。空白和文本值将被忽略。RequiredcriteriaAnumber,expression,cellr......
  • JavaScript函数大全 集合
    JavaScript函数大全集合javascript提供了许多函数供开发人员使用,下面给出一个简单介绍,更详细的信息请参考VisualInterDev提供的在线帮助。javascript函数一共可分为五类:·常规函数·数组函数·日期函数·数学函数·字符串函数1.常规函......