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