简体   繁体   English

无法在Jupyter Notebook单元中打印bash变量

[英]Cannot print bash variables in Jupyter notebook cell

I am trying to combine Python and bash commands in one Jupyter notebook shell but only python variables are working but not the bash commands using those python variables. 我正在尝试在一个Jupyter笔记本外壳中结合使用Python和bash命令,但是只有python变量有效,而使用这些python变量的bash命令却没有。 Here is an example 这是一个例子

username="test_username"
password = getpass.getpass(prompt='Enter your password: ')
!echo "$username"
!echo "$password"
!AUTH_HEADER=$(curl -su "$username":"$password" "https://de.cyverse.org/terrain/token" | jq -r .access_token)
!export AUTH_HEADER
!echo "$AUTH_HEADER"

When I tried to execute this in Jupyter notebook cell, this is what I see with an empty blank line for AUTH_HEADER variable. 当我尝试在Jupyter笔记本电脑单元中执行此操作时,这是我看到的带有空空白行的AUTH_HEADER变量。

Enter your password:  ·············
test_username
test123

I also able to echo username and password but not the AUTH_HEADER . 我还可以回应用户名和密码,但不能AUTH_HEADER I don't understand what I am doing wrong here. 我不明白我在做什么错。 Any help is appreciated. 任何帮助表示赞赏。

I suppose each run off shell command !cmd starts a new bash instance. 我想每一个运行shell命令!cmd启动一个新的bash实例。 Thus the variable AUTH_HEADER is not shared between bash instances. 因此,变量AUTH_HEADER在bash实例之间不共享。 Try run cell with bash and run your script there like below 尝试使用bash运行cell并在其中运行脚本,如下所示

%bash
AUTH_HEADER=$(curl -su "$username":"$password"  "https://de.cyverse.org/terrain/token" | jq -r .access_token)
export AUTH_HEADER
echo "$AUTH_HEADER"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM