sed: -e expression #1, char 1: unknown command: `f'
- tool
- sed
- exit code
- 1
- versions
- GNU sed 4.x
- os
- linux, macos, windows
sed: -e expression #1, char 1: unknown command: f'means the first character sed read in the script is not a command. In sed, a script is a list of commands such ass, p, d, yora, optionally preceded by an address. A bare word like foois not a command, so sed stops at its first letter. Uses/foo/bar/to replace text or/foo/p` to print matching lines.
Reproduce it
sed 'foo' /dev/nullOutput · exit 1
sed: -e expression #1, char 1: unknown command: `f'
printf 'a\n' | sed 's/a/b/;f'Output · exit 1
sed: -e expression #1, char 8: unknown command: `f'
The first example fails at char 1, the second at char 8, the f after the semicolon. GNU sed counts the position from the start of the failing -e expression, and the expression number counts every -e and script file given.
How to read the message
| Part | Meaning |
|---|---|
-e expression #1 | The first script given with -e, or the one positional script |
char 1 | The character position, counted from 1, where parsing stopped |
unknown command: f'` | The character at that position, shown in quotes |
When the script comes from a file with -f, the message names the file and line instead: sed: file script.sed line 2: unknown command.
Cause 1: the script has no command
People often type the text they want to find and expect sed to search for it, like grep. sed needs a command to act on the text. Either replace it with s, or print the lines that match:
printf 'foo\nbar\n' | sed 's/foo/baz/'
printf 'foo\nbar\n' | sed -n '/foo/p'Output
baz bar foo
Cause 2: quoting changed the script
Double quotes let the shell expand $variables and backticks before sed sees the script. If the variable is empty or holds a space or a slash, the script becomes something else. Use single quotes for fixed scripts. When you must insert a variable, switch to double quotes on purpose and check what sed receives with echo:
word=bar
printf 'foo\n' | sed "s/foo/$word/"Output
bar
A slash inside the variable ends the s command early and produces other errors: unknown option to s or unterminated s command. Choose another delimiter such as | in that case.
Cause 3: several commands without a separator
Commands must be separated by a semicolon or a newline. sed 's/a/b/ s/c/d/' makes sed read the text after the first s command as flags, and it fails with unknown option to s; other commands in that position give extra characters after command. With a separator, or with one -e per command, sed accepts the script:
printf 'ac\n' | sed -e 's/a/b/' -e 's/c/d/'Output
bd
Fix it
- Read the character in the message and find it in your script.
- Make sure it is a command letter, or part of an address or a regular expression that belongs to one.
- Run the script on a small sample with
printf 'text\n' | sed '...'before running it on a file, and use-ionly after the output is right.
Platform note
The wording above is GNU sed, the default on Linux and in Git Bash. BSD sed on macOS uses different messages for the same mistakes and requires an argument after -i (-i '').
FAQ
What does char 1 mean in the message?
It is the position, inside the script, of the character sed could not read as a command. char 1 means the very first character of the first -e expression.
Why does the message show the same letter I typed?
sed prints the unknown command character in quotes, for example `f'. If the character is different from what you typed, the shell changed the text, so check quoting and variable expansion.
Does BSD sed on macOS print the same message?
BSD sed words its errors differently. The cause is the same: the script text is not valid sed.