2

Suppose I have the following code in a StackOverflow response:

$ export FLASK_APP=main.py
$ export FLASK_DEBUG=1
$ python -m flask run

Is there an easy way to copy and paste this without the $ signs, so I can directly paste this into my terminal?

Jonathan Ma
  • 556
  • 6
  • 20
  • Paste it into `vim` then you can just whack off the first character [like this](https://stackoverflow.com/a/55507361/2221001). At any rate you will have to put it into some text editor and manipulate (or hit the file with `sed` or something). – JNevill Oct 03 '19 at 19:43
  • You might want to look at the `fc` command in conjunction with JNevill's suggestion. – chepner Oct 03 '19 at 20:53
  • In zsh, you can simply make a dollar function that serves as a pass-thru: `function $ { "$@" }`. – mattmc3 Feb 13 '23 at 23:20

1 Answers1

3

You could do:

. <( sed 's/^\$ //' <<'PASTE'
**paste here**
PASTE
)

Or, make that into a function:

undollar() { . <( sed 's/^\$ //' ); }

Than you use that like

$ undollar<hit enter>
<paste here>
<hit Ctrl+D>

Both of these approaches use the . command, so effects are seen in the current shell: for example with the commands you list, the FLASK_APP and FLASK_DEBUG environment variables remain in the shell.


As noted by Charles Duffy, old versions of bash cannot source a process substitution: see Why source command doesn't work with process substitution in bash 3.2?

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    Note that some versions of bash have a bug when sourcing output from a process substitution -- see https://stackoverflow.com/questions/32596123/why-source-command-doesnt-work-with-process-substitution-in-bash-3-2 – Charles Duffy Oct 03 '19 at 19:57