Skip to content

How can i uncomment a line with echo or printf

A noob question, but how can i uncomment a line via the command line (w/o a text editor), i'm just kinda curious how to do it that way. Think of echo/printf but don't know that well how to use them.

Edit: If you didn't understood, I meant to uncomment a line like # %wheel ALL=(ALL:ALL) ALL and any shell command, not only echo/printf

Edit 2: Think I have the answer now :D

Edited 19 Aug 2022. We only hold this version.

4 points25 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
4 ยท 56% up
Kind
Text
ID
wsba20

Author

2
posts
6
comments
0
media
View profile

26 comments

You mean something like this echo [magic string] > file and it modifies the file to uncomment a line? That won't work, you can only append data or overwite the whole file using echo and > or >> operators.

To do more advanced editing like uncommenting you can use sed.

13 points

I don't mean those commands only btw.. any command like cut, sed or such will do I'm just talking about the lines like # %wheel ALL=(ALL:ALL) ALL

0 points

sed -i '/^# %wheel/s/# //' file uncomments all lines in a file starting with # %wheel/

3 points

For the general case, use sed:

sed -i '/# %wheel/s/^# //' file.txt

For the sudoers file, please use sudo visudo before you lock yourself out like I did.

11 points

I know this is not on topic or answering your question either, but that looks like your sudoers file and I'd recommend only editing it with visudo and nothing else, let alone a script.

6 points

echo and printf are not what you should use. you could use sed to do a search and replace for every instance of # with an empty string.

6 points

Are you saying that you have a file, and you want to modify the file so that every line that has an echo or printf is uncommented?

3 points

maybe you're the noob

-6 points

Perhaps, but I'd still like to understand the question. The only other interpretation I can think of is to do some fancy shell scripting to uncomment a line using echo or printf, but I can't think of an obvious way to do that since I'd prefer using sed or something like that.

3 points

So the easiest way would be to just add a new line without the comment. So echo %wheel ALL=(ALL:ALL) ALL >> file.conf

3 points

yh i know can also do that but was just curious for another way

0 points

You can uncomment the respective line with sed. The issue with that approach is that the commented line needs to exist for the command to have an effect and that sed will "fail" silently. The sed expression s/pattern/replacement/ will replace 'pattern' with 'expression' if and only if it encounters it. If it doesn't encounter the pattern it will still exit successfully, having found nothing to do.
Therefore simply appending the line you want to have is the safer and quicker solution.

1 points

"echo" and "printf" are shell commands. they don't comment out stuff. You probably added those in front of the commands you wanted removed because you didn't want them executed. And that works, but it is not what those commands are for. They print strings out to the terminal the script is executed in. If you write "echo test", the script will print out "test".

To actually comment out a line from a shell script, prefix the line with a hash character ("#").

1 points
unknown author

Deleted before we saved it.

-1 points

I think you would want to use 'sed' to accomplish this

1 points

In my opinion instead editing the file /etc/sudoers the best way to do it is something like that:

mkdir -p /etc/sudoers.d
echo "%wheel ALL=(ALL:ALL) ALL" > /etc/sudoers.d/01-wheel

P.S. but you have to check if the next line exists into the file /etc/sudoers

@includedir /etc/sudoers.d
1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

โ†orโ†’
Previous / next file
โ†‘orโ†“
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.