avoid dollar $ interpolation in shell commands

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

avoid dollar $ interpolation in shell commands

Christian Theil Have
Hi,

I've been trying to create a shell command that refers to an environment variable, e.g., 

echo $PATH

Julia will interpolate $ in shell commands in backticks to Julia variables, i.e., `echo $PATH`, will look for a Julia variable PATH. 
What can I do if I really want to insist having a shell command that includes a (non-quoted) dollar-sign? Currently,
my workaround is `sh -c "echo \$PATH"`, but this is not really satisfactory.

Best,
Christian
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: avoid dollar $ interpolation in shell commands

Stefan Karpinski
Are you trying to get the shell to expand $PATH but not Julia?

On Wed, Nov 2, 2016 at 5:40 AM, Christian Theil Have <[hidden email]> wrote:
Hi,

I've been trying to create a shell command that refers to an environment variable, e.g., 

echo $PATH

Julia will interpolate $ in shell commands in backticks to Julia variables, i.e., `echo $PATH`, will look for a Julia variable PATH. 
What can I do if I really want to insist having a shell command that includes a (non-quoted) dollar-sign? Currently,
my workaround is `sh -c "echo \$PATH"`, but this is not really satisfactory.

Best,
Christian

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: avoid dollar $ interpolation in shell commands

Simon Byrne
In reply to this post by Christian Theil Have
Perhaps not quite what you had in mind, but

`echo $ENV["PATH"]`

should work

On Wednesday, 2 November 2016 10:43:47 UTC, Christian Theil Have wrote:
Hi,

I've been trying to create a shell command that refers to an environment variable, e.g., 

echo $PATH

Julia will interpolate $ in shell commands in backticks to Julia variables, i.e., `echo $PATH`, will look for a Julia variable PATH. 
What can I do if I really want to insist having a shell command that includes a (non-quoted) dollar-sign? Currently,
my workaround is `sh -c "echo \$PATH"`, but this is not really satisfactory.

Best,
Christian
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: avoid dollar $ interpolation in shell commands

Stefan Karpinski
I think that needs some additional parens: `echo $(ENV["PATH"])`. This is in fact, exactly what you need – Julia commands in backticks are not run by a shell so there is no shell to expand any environment variables, only Julia can expand them.

On Wed, Nov 2, 2016 at 7:02 AM, Simon Byrne <[hidden email]> wrote:
Perhaps not quite what you had in mind, but

`echo $ENV["PATH"]`

should work

On Wednesday, 2 November 2016 10:43:47 UTC, Christian Theil Have wrote:
Hi,

I've been trying to create a shell command that refers to an environment variable, e.g., 

echo $PATH

Julia will interpolate $ in shell commands in backticks to Julia variables, i.e., `echo $PATH`, will look for a Julia variable PATH. 
What can I do if I really want to insist having a shell command that includes a (non-quoted) dollar-sign? Currently,
my workaround is `sh -c "echo \$PATH"`, but this is not really satisfactory.

Best,
Christian

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: avoid dollar $ interpolation in shell commands

Christian Theil Have
Thank you for the responses and my apologies for not being entirely clear: Actually, the $PATH variable was just to provide an example. I really did want to construct a Cmd with an unquoted dollar in it.
The reason why I want do such a silly thing is that I have a toying with a package (https://github.com/cth/QsubCmds.jl) which generates shell scripts from Cmd's that are run through sun grid engine, so
since the commands are not executed by Julia anymore, but a shell, it makes sense to allow interaction with shell variables. I do not know whether there could be any other use case for this (probably not).

I get if this is not possible for the reason that there is no shell to expand variables when running Cmds from Julia, but I just wondered if there are some escaping mechanism that makes it possible to construct such commands.

Best,
Christian



Den onsdag den 2. november 2016 kl. 12.23.00 UTC+1 skrev Stefan Karpinski:
I think that needs some additional parens: `echo $(ENV["PATH"])`. This is in fact, exactly what you need – Julia commands in backticks are not run by a shell so there is no shell to expand any environment variables, only Julia can expand them.

On Wed, Nov 2, 2016 at 7:02 AM, Simon Byrne <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="EbJycYjbAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">simon...@...> wrote:
Perhaps not quite what you had in mind, but

`echo $ENV["PATH"]`

should work

On Wednesday, 2 November 2016 10:43:47 UTC, Christian Theil Have wrote:
Hi,

I've been trying to create a shell command that refers to an environment variable, e.g., 

echo $PATH

Julia will interpolate $ in shell commands in backticks to Julia variables, i.e., `echo $PATH`, will look for a Julia variable PATH. 
What can I do if I really want to insist having a shell command that includes a (non-quoted) dollar-sign? Currently,
my workaround is `sh -c "echo \$PATH"`, but this is not really satisfactory.

Best,
Christian

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: avoid dollar $ interpolation in shell commands

Stefan Karpinski
You can make a command with quoted `$` like this: `echo \$x` or `echo '$x'`.

On Wed, Nov 2, 2016 at 8:00 AM, Christian Theil Have <[hidden email]> wrote:
Thank you for the responses and my apologies for not being entirely clear: Actually, the $PATH variable was just to provide an example. I really did want to construct a Cmd with an unquoted dollar in it.
The reason why I want do such a silly thing is that I have a toying with a package (https://github.com/cth/QsubCmds.jl) which generates shell scripts from Cmd's that are run through sun grid engine, so
since the commands are not executed by Julia anymore, but a shell, it makes sense to allow interaction with shell variables. I do not know whether there could be any other use case for this (probably not).

I get if this is not possible for the reason that there is no shell to expand variables when running Cmds from Julia, but I just wondered if there are some escaping mechanism that makes it possible to construct such commands.

Best,
Christian



Den onsdag den 2. november 2016 kl. 12.23.00 UTC+1 skrev Stefan Karpinski:
I think that needs some additional parens: `echo $(ENV["PATH"])`. This is in fact, exactly what you need – Julia commands in backticks are not run by a shell so there is no shell to expand any environment variables, only Julia can expand them.

On Wed, Nov 2, 2016 at 7:02 AM, Simon Byrne <[hidden email]> wrote:
Perhaps not quite what you had in mind, but

`echo $ENV["PATH"]`

should work

On Wednesday, 2 November 2016 10:43:47 UTC, Christian Theil Have wrote:
Hi,

I've been trying to create a shell command that refers to an environment variable, e.g., 

echo $PATH

Julia will interpolate $ in shell commands in backticks to Julia variables, i.e., `echo $PATH`, will look for a Julia variable PATH. 
What can I do if I really want to insist having a shell command that includes a (non-quoted) dollar-sign? Currently,
my workaround is `sh -c "echo \$PATH"`, but this is not really satisfactory.

Best,
Christian


Loading...