title: embedding short python scripts inside a bash script
body: <p>I'd like to embed the text of short python scripts inside of a bash script, for use in say, my <code>.bash_profile</code>. What's the best way to go about doing such a thing?</p>
  
  <p>The solution I have so far is to call the python interpreter with the <code>-c</code> option, and tell the interpreter to <code>exec</code> whatever it reads from <code>stdin</code>. From there, I can build simple tools like the following, allowing me to process text for use in my interactive prompt:</p>
  
  <pre><code>function pyexec() {
      echo "$(/usr/bin/python -c 'import sys; exec sys.stdin.read()')"
  }
  
  function traildirs() {
      pyexec &lt;&lt;END
  trail=int('${1:-3}')
  import os
  home = os.path.abspath(os.environ['HOME'])
  cwd = os.environ['PWD']
  if cwd.startswith(home):
      cwd = cwd.replace(home, '~', 1)
  parts = cwd.split('/')
  joined = os.path.join(*parts[-trail:])
  if len(parts) &lt;= trail and not joined.startswith('~'):
      joined = '/'+joined
  print joined
  END
  }
  
  export PS1="\h [\$(traildirs 2)] % "
  </code></pre>
  
  <p>This approach smells slightly funny to me though, and I'm wondering what alternatives to doing it this way might be. </p>
  
  <p>My bash scripting skills are pretty rudimentary, so I'm particularly interested to hear if I'm doing something silly from the bash interpreter's perspective.</p>