As we all know, using echo and cooperating with command redirection is a shortcut that implements writing information to a file. This article describes how to use the echo command with the sudo command.
implementations write information to files that only the system administrator has permission to do. For example, to write to the Test.asc file random content, you can: $ echo "info" > test.asc# or echo "Information" >> TEST.ASC below, if the TEST.ASC permission is set to only the root user has permission to enter Line write operation: $ sudo chown root.root test.asc then we use sudo to write the information to the TEST.ASC file after modifying the permission again with the echo command: $ sudo echo "Another line of information" >> TE St.asc-bash:test.asc:permission denied at this point, you can see that bash refuses to do so, saying it's not enough authority. This is because the redirect symbol ">" and ">>" are also commands for bash. We use sudo just to have the echo command root, but without the ">" and ">>" commands having root privileges, bash will assume that neither command has permission to write information to the TEST.ASC file. There are two ways to solve this problem. The first is to use the "sh-c" command, which allows bash to execute a string as a complete command, so that the scope of sudo can be extended to the entire command. The specific usage is as follows: $
sudo sh-c "Echo ' another line of information ' >> TEST.ASC"
this way sudo executes a shell that executes the previous command in the root-privileged shell without any more permissions issues. Another approach is to use the pipe and tee commands, which can read information from standard input and write it into standard output or a file, using the following: $ echo "Third message" | sudo tee-a test.asc Note that the "-a" option of the tee command is equivalent to the ">>" command, and if this option is removed, the tee command is equivalent to the ">" command.
(GO) Linux sudo redirection to write information in files that only the system administrator has permission to manipulate