Ruby Command Line Options

Ruby is generally run from the command line as follows:

$ ruby [ options ] [.] [ programfile ] [ arguments ... ]

The interpreter can be invoked with the following options to control the interpreter's environment and behavior.

OptionDescription
-aWhen used with -n or -p, enables auto split mode. See the -n and -p options.
-cChecks syntax only, without executing the program.
-C dirChanges directory before execution (equivalent to -X).
-dEnables debug mode (equivalent to -debug).
-F patSpecifies pat as the default split pattern ($;).
-e progSpecifies prog as the program to execute from the command line. Multiple -e options can be specified to execute multiple programs.
-hDisplays an overview of command line options.
-i [ ext]Rewrites file contents as program output. The original file is saved with extension ext. If ext is not specified, the original file is deleted.
-I dirAdds dir as a directory for loading libraries.
-K [ kcode]Specifies multibyte character set encoding. e or E corresponds to EUC (extended Unix code), s or S corresponds to SJIS (Shift-JIS), u or U corresponds to UTF-8, a, A, n or N corresponds to ASCII.
-lEnables automatic line ending processing. Removes a newline from input lines and appends a newline to output lines.
-nPlaces the code in an input loop (like in while gets; ... end).
-0[ octal]Sets the default record separator ($/) to octal. If octal is not specified, it defaults to \0.
-pPlaces the code in an input loop. Outputs the value of variable $_ after each iteration.
-r libUserequireto loadlibas a library before execution.
-sInterprets any argument matching the -xxx pattern between the program name and filename arguments as a switch, and defines the corresponding variable.
-T [level]Sets the safety level, performing taint check (if level is not specified, the default value is 1).
-vDisplays version and enables verbose mode.
-wEnables verbose mode. If no program file is specified, reads from STDIN.
-x [dir]Deletes text before the #!ruby line. If specifieddir, changes the directory todir。
-X dirChanges directory before execution (equivalent to -C).
-yEnables parser debug mode.
--copyrightDisplays copyright notice.
--debugEnables debug mode (equivalent to -d).
--helpDisplays an overview of command line options (equivalent to -h).
--versionDisplays version.
--verboseEnables verbose mode (equivalent to -v). Sets $VERBOSE to true.
--yydebugEnables parser debug mode (equivalent to -y).

Single-character command line options can be combined. The following two lines express the same meaning:

$ ruby -ne 'print if /Ruby/' /usr/share/bin


$ ruby -n -e 'print if /Ruby/' /usr/share/bin
Other Extensions