Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

71%
+3 −0
Q&A Replacing the shebang in a script

If there is a standard or well-known way way to accomplish this, someone else will have to provide it. But if you're open to more... hacky alternatives... Escaping the delimiter character / can ce...

posted 7mo ago by Hackysack‭

Answer
#1: Initial revision by user avatar Hackysack‭ · 2026-02-18T22:27:58Z (7 months ago)
If there is a standard or well-known way way to accomplish this, someone else will have to provide it. But if you're open to more... _hacky_ alternatives...

Escaping the delimiter character `/` can certainly be a pain, especially when that character is used extensively throughout one or both of the patterns in `sed 's/regex/replacement/'`. That being said, we're not actually limited to using `/`. According to `man sed`, it can be any character (almost, anyway):

```
/regexp/
    Match lines matching the regular expression regexp.  Matching is performed on the current pattern space, which can be modified with commands such as ‘‘s///''.
\cregexpc
    Match lines matching the regular expression regexp.  The c may be any character.
```

So, instead of this:
```bash
sed 's/\/path\/to\/some\/file/\/path\/to\/some\/other\/file/' infile.txt
```
we can instead use:
```bash
sed 's|/path/to/some/file|/path/to/some/other/file|' infile.txt
```

(It's beyond the scope of this answer, but this also works with `sed`'s `y/source/dest/` command, meaning `sed 'y#abc#XYZ#'` would be just as valid as `sed 'y/abc/XYZ/'`)

Additionally, you can force `sed` to do this for only the first line, by prepending a `1` to the command, such as:
```bash
sed '1s/regex/replacement/'
```

I took the liberty of crafting a `sed` command to accomplish the task you described in your question. It matches the first line only if it starts with `#!` and replaces it with `${shebang}`, writing the entirety of the output to STDOUT as normal.
```bash
sed "1s|#!.*|${shebang}|" infile.txt
```

If you want to match the first line unconditionally, then simply remove the `#!.*`. If you want to match a specific subset of shebangs, a more involved regex pattern could be used.