bash alias 多引号问题

一句话:

像python那样,在''里面使用'\'abc\'',在bash中不行

 

需要把

\'

换成

'\''

这样问题解决

 

You can make that command into a Bash alias by editing your shell configuration file (typically ~/.bashrc or ~/.bash_aliases).

Here’s the step-by-step procedure:


1. Open your .bashrc (or .bash_aliases) file

nano ~/.bashrc

2. Add the alias definition

At the end of the file, add a line like this:

alias qbstat='ps aux | grep qbittorrent | awk '\''{printf "%-7s %-7s %-20s\n", $1, $2, $12}'\'''

Explanation:

  • You need to escape single quotes properly because of the nested quoting.

  • The '\'' sequence is how you embed a literal ' inside a single-quoted string in Bash.


3. Reload your shell configuration

source ~/.bashrc

4. Use your alias

qbstat

It should print output formatted as:

USER    PID     COMMAND
user1   1234    qbittorrent-nox

⚙️ Optional improvement

If you want to automatically exclude the grep qbittorrent process from showing up, modify the alias like this:

alias qbstat='ps aux | grep qbittorrent | grep -v grep | awk '\''{printf "%-7s %-7s %-20s\n", $1, $2, $12}'\'''

Would you like me to show you a more robust variant that doesn’t rely on grep (e.g., using pgrep or ps -C qbittorrent)? That’s cleaner and safer for scripting.

 

posted @ 2025-10-12 01:07  profesor  阅读(5)  评论(0)    收藏  举报