课程: Linux: System Information and Directory Structure Tools
Learning Linux command syntax - Linux教程
课程: Linux: System Information and Directory Structure Tools
Learning Linux command syntax
- [Instructor] The Linux command line can be complex but understanding the syntax of a Linux command can be beneficial in becoming proficient. The standard Linux command syntax is command [options] and then <arguments>. The command [options] and <arguments> are separated by blank spaces. Options are by definition optional. Options generally come in two forms, a single letter preceded by a hyphen and the GNU long format of a word preceded by two hyphens. Options can also have arguments. For instance, in this case, the argument to the -i option is in.txt. Options and their arguments do not have to appear as separate tokens. For instance, -iin.txt can exist without a space delimiter. The command expects the characters that follow a valid option to be the argument to the option until the next space. Although I'm not a fan of this format as it makes the command line harder to read. Long format options with arguments are generally written with an equal sign between the option and the option argument. The combination makes an easy to understand command line although it can be very long. Single letter options can be stacked as long as they have no argument. So instead of typing in -l -h you can type -lh. Single letter options and GNU long format options can be combined as long as they're separated by a space. Arguments are usually provided last on the command line. It's important to understand that options are a part of the program, so they're known. But arguments are the objects that the program will process and are unknown, that's as best for them to be last. For instance, arguments could be a list of files such as file1, file2, and file3, or they could be a wild card matching any number of files. It's the shell that expands the wild card into a list of arguments and not the command. In the case of the asterisk it matches all files in the current directory. Those file names are then passed through the command by the shell as if the user typed them in manually. Many commands require at least one argument but some assume an argument if one is not provided. For instance, a tool like ls will list the names of files alphabetically in the current directory if options and arguments are not provided. This would be the same as typing in ls . as the dot is the path shortcut to the current directory. What we've covered here is standard Linux and GNU command syntax. However, due to the nature of Linux and its 50 years of historical Unix roots, there are exceptions. You may have commands with what looks like options that are full words but do not include double hyphens and are, in fact, subcommands that also have their own arguments. There may also be single letter options that don't require proceeding hyphens, or even commands that have single letter options which have their own arguments at the end of the line. The options and arguments to some commands are extraordinarily complex and have to be provided in a certain mysterious order. Thankfully, most commands follow the rules.