shell command # can't be carry out when it not used for comments on colab

159 Views Asked by At

I'm confused about this code! Why # cant't play a role that takes the length of a string?

 string="abcd"

 !echo ${#string}

In fact, the code behind # has become commented and cannot be executed!

Any advice?

1

There are 1 best solutions below

1
jakevdp On

This works correctly, but you cannot mix python and bash variables in this way. Try this instead:

!string="abcd" && echo ${#string}

The two statements have to be on the same line because in IPython, each ! statement opens a temporary subshell and variables are not persisted between shells. If you want to use multiline bash programs, you can use the %%bash cell magic instead:

%%bash
string="abcd"
echo  ${#string}