How to escape strings for terminal in Ruby?
问题 I am attempting to start mplayer. My filename contains spaces and these should be escaped. This is the code I am using: @player_pid = fork do exec "/usr/bin/mplayer #{song.file}" end where #{song.file} contains a path like "/home/example/music/01 - a song.mp3" . How can I escape this variable properly (and possible other weird characters that the title may contain) so the terminal will accept my command? 回答1: Shellwords should work for you :) exec "/usr/bin/mplayer %s" % Shellwords.escape