脚本多单引号致SSH修改ulimit失效,求修改及替代方案
Got it, let's tackle this quote nesting issue—it's super common when writing SSH scripts that run commands with quotes on remote servers. The problem is that shell doesn't let you nest single quotes directly, so we need workarounds that keep the command intact for the remote server. Here are a few reliable solutions tailored to your ulimit modification task:
1. Split Single Quotes with '\''
This is a neat trick to embed single quotes inside a single-quoted string. The sequence '\'' works like this:
'closes the current single-quoted block\'adds a literal single quote'reopens the single-quoted block
For example, if your original broken command was trying to append a line to limits.conf over SSH:
# Broken version (nested single quotes cause errors) ssh user@your-server 'echo '* soft nofile 65536' >> /etc/security/limits.conf'
Fix it with the split quote sequence:
ssh user@your-server 'echo '\''* soft nofile 65536'\'' >> /etc/security/limits.conf'
2. Use Double Quotes for the Outer Wrapper
If your command doesn't have variables that need to be expanded locally, you can wrap the entire remote command in double quotes, and escape internal double quotes with \. This avoids single quote nesting entirely:
ssh user@your-server "echo \"* soft nofile 65536\" >> /etc/security/limits.conf"
Note: If you do need to pass local variables to the remote command, you can leave them unescaped (they'll expand locally), and escape remote variables with \$ if needed. For example:
LOCAL_LIMIT=65536 ssh user@your-server "echo \"* soft nofile $LOCAL_LIMIT\" >> /etc/security/limits.conf"
3. Use a Here Document (Best for Multi-Command Scripts)
If you're running multiple commands (like setting both soft and hard limits, plus applying the temporary ulimit), a here document (<< EOF) is way cleaner and avoids all quote headaches. Add single quotes around EOF to prevent local variable expansion (so everything runs exactly as written on the remote server):
ssh user@your-server << 'EOF' # Append ulimit rules to limits.conf echo '* soft nofile 65536' >> /etc/security/limits.conf echo '* hard nofile 65536' >> /etc/security/limits.conf # Apply temporary ulimit for the current session ulimit -n 65536 EOF
If you need to use local variables in the remote commands, just remove the single quotes around EOF:
LOCAL_LIMIT=65536 ssh user@your-server << EOF echo "* soft nofile $LOCAL_LIMIT" >> /etc/security/limits.conf echo "* hard nofile $LOCAL_LIMIT" >> /etc/security/limits.conf ulimit -n $LOCAL_LIMIT EOF
Bonus: Handling Sudo (If You Need Root Access)
If modifying limits.conf requires sudo, you'll need to wrap the command in sudo sh -c to preserve the quotes. Using the split quote method:
ssh user@your-server 'sudo sh -c '\''echo "* soft nofile 65536" >> /etc/security/limits.conf'\'''
Or with a here document:
ssh user@your-server sudo bash << 'EOF' echo '* soft nofile 65536' >> /etc/security/limits.conf echo '* hard nofile 65536' >> /etc/security/limits.conf EOF
Any of these methods should fix your quote nesting issue. The here document is my go-to for longer scripts since it's easier to read and maintain!
内容的提问来源于stack exchange,提问作者Sulata Kumari

