How to pass the Python variable to c shell script


I am using Centos 7.0 and PyDEv in Eclipse. I am trying to pass the variable in Python into c shell script. But I am getting error:

This is my Python script named raw2waveconvert.py

num = 10 print(num) import subprocess subprocess.call(["csh", "./test1.csh"])

Output/Error when I run the Python script:

10 num: Undefined variable.

The file test1.csh contains:

#!/bin/csh set nvar=`/home/nishant/workspace/codec_implement/src/NTTool/raw2waveconvert.py $num` echo $nvar


Okey, so apparently it's not so easy to find a nice and clear duplicate. This is how it's usually done. You either pass the value as an argument to the script, or via an environmental variable.

The following example shows both ways in action. Of course you can drop whatever you don't like.

import subprocess import shlex var = "test" env_var = "test2" script = "./var.sh" #prepare a command (append variable to the scriptname) command = "{} {}".format(script, var) #prepare environment variables environment = {"test_var" : env_var} #Note: shlex.split splits a textual command into a list suited for subprocess.call subprocess.call( shlex.split(command), env = environment )

This is corresponding bash script, but from what I've read addressing command line variables is the same, so it should work for both bash and csh set as default shells.


#!/bin/sh echo "I was called with a command line argument '$1'" echo "Value of enviormental variable test_var is '$test_var'"


luk32$ python3 subproc.py I was called with a command line argument 'test' Value of enviormental variable test_var is 'test2'

Please note that the python interpreter needs to have appropriate access to the called script. In this case var.sh needs to be executable for the user luk32. Otherwise, you will get Permission denied error.

I also urge to read docs on <a href="https://docs.python.org/3/library/subprocess.html" rel="nofollow">subprocess</a>. Many other materials use shell=True, I won't discuss it, but I dislike and discourage it. The presented examples should work and be safe.


subprocess.call(..., env=os.environ + {'num': num})


The only way to do what you want here is to export/pass the variable value through the shell environment. Which requires using the env={} dictionary argument.

But it is more likely that what you <em>should</em> do is pass arguments to your script instead of assuming pre-existing variables. Then you would stick num in the array argument to subprocess.call (probably better to use check_call unless you know the script is supposed to fail) and then use $1/etc. as normal.


  • How to add power law likelihood to Netlogo Model
  • can't compile openssl because of 'cl' is not recognized
  • Invalidate node cache when using Jest
  • Batch file working differently if ran in administrator mode
  • Merge list of objects into consistent list based on common matching attribute in Python
  • Insert records if not exist SQL Server 2005
  • Zend Framework bassed projects
  • Unique Permutations - with exceptions
  • ZipList with Scalaz
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Outputting SharePoint Hyperlink Column as URL
  • Doctrine/Symfony entity generator and generating entity from one table
  • Cloud Code function running twice
  • Uncaught TypeError: $(…).select2 is not a function
  • Android changing fragment order inside FragmentPagerAdapter
  • Jquery popup on mouse over of calendar control
  • How to view images from protected folder with php?
  • How do I superscript characters in a UIButton?
  • Assign variable to the value in HTML
  • How do I get HTML corresponding to current DOM tree?
  • Display images in Django
  • How to use carriage return with multiple line?
  • d3 v4 drag and drop with TypeScript
  • Why does access(2) check for real and not effective UID?
  • How to create a file in java without a extension
  • Illegal mix of collations for operation for date/time comparison
  • What is Eclipse's Declaration View used for?
  • Resize panoramic image to fixed size
  • Controls, properties, events and timers running in design time
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • Updated Ionic CLI but shows previous version (Windows)
  • Importing jscolor library in angular 2
  • R: gsub and capture
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?