Skip to content

why does rg '<,'> file.txt clear the contents of file.txt? #2888

Answered by okdana
AnthonyGithubCorner asked this question in Q&A
Discussion options

You must be logged in to vote

This is a shell command typo that ripgrep can't do anything about. The shell interprets the quoting and redirect syntax before ripgrep is even executed, all it knows is that you've told it to search for the pattern <, and output the results to a file. Even if it could see the raw shell command, it's perfectly valid, so there's no way for it (or the shell) to figure out that you meant to type something else

If you want, you can enable the noclobber option in your shell profile to make it so that > isn't allowed to overwrite the contents of existing files:

# In bash:
set -o noclobber

# In zsh:
setopt no_clobber
# Also set this to make it work like bash, where you can still use >> to create…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2887 on September 12, 2024 13:07.