Quantcast
Channel: User martineau - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by martineau for How to pass Python variable into bash commands?

$
0
0

You can use a so-called "f-string" to replace the variable references with their values.

context = {'nickname': 'foobar'}bash_code = f'''echo "/common_home/{context['nickname']}  /tmp/back/{context['nickname']}  none bind 0 0" | sudo tee --append /etc/fstabsudo mkdir /tmp/{context['nickname']}  /tmp/back/{context['nickname']}'''print(bash_code)subprocess.run(['bash', '-c', bash_code], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Value printed:

echo "/common_home/foobar  /tmp/back/foobar  none bind 0 0" | sudo tee --append /etc/fstabsudo mkdir /tmp/foobar  /tmp/back/foobar

Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>