[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-571885-105":3,"detail-sidebar-cat-1-en-105":81,"doc-detail-571885-en":127},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":74,"head_meta":76,"extra_data":78,"updated_unix":80},105,"en","bash-cheat-sheet-reference-commands-and-syntax","Bash Cheat Sheet - reference - commands and syntax","","Bash Cheat Sheet by John Stowers provides concise tables covering common Bash shell usage across tests, conditional expressions, string and numeric comparisons, boolean operators, and typical control-flow patterns for if/then/elif/fi and nested if statements. It also summarizes shell built-in variables, file and pattern testing with flags like -r/-w/-x/-f/-d/-e/-s, wildcard matching, quoting rules, grouping, case statements, loops, and essential I/O redirection and piping forms for standard streams and file descriptors.",{"@graph":14,"@context":73},[15,34,56],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/template/","Template",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/template/presentations/","Presentations",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/template/bash-cheat-sheet-reference-commands-and-syntax/571885/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":49,"encodingFormat":47,"isAccessibleForFree":50,"interactionStatistic":51},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/bash-cheat-sheet-reference-commands-and-syntax/571885.png","ImageObject",442,249,{"name":42,"@type":43},"Fahsai","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-10-22","2026-10-13",true,{"@type":52,"interactionType":53,"userInteractionCount":55},"InteractionCounter",{"@type":54},"ViewAction",6,{"@type":57,"mainEntity":58},"FAQPage",[59,65,69],{"name":60,"@type":61,"acceptedAnswer":62},"How do you check whether a file is readable, writable, executable, or a directory in Bash?","Question",{"text":63,"@type":64},"Use test expressions like [-r file] for readable, [-w file] for writable, [-x file] for execute access, [-d file] for a directory, and other flags such as -f for ordinary files and -e for existence.","Answer",{"name":66,"@type":61,"acceptedAnswer":67},"What Bash operators are used to compare strings, numbers, and combine boolean conditions?",{"text":68,"@type":64},"Strings use equality/inequality such as [s1 = s2] and [s1 != s2], numbers use -eq/-ne/-lt/-le/-gt/-ge, and boolean combinations use !, -a for and, and -o for or.",{"name":70,"@type":61,"acceptedAnswer":71},"How does Bash handle quoting and I/O redirection in the provided cheatsheet?",{"text":72,"@type":64},"Quoting rules include escaping with \\, command substitution with `cmd`, literal double quotes with \"...\" after interpreting $, `...`, and \\, and fully literal single quotes '...'. I/O redirection covers >, \u003C, >>, pipelines with |, and descriptor-based redirection like n > file, n >& m, and heredoc with \u003C\u003C tag.","https://schema.org",{"og:url":32,"og:type":75,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":77,"canonical":32},"index,follow",{"doc_id":79,"site_id":7},571885,1792338318,{"code":4,"msg":82,"data":83},"success",[84,88,93,98,103,108,113,118,123],{"id":85,"doc_module":22,"doc_module_name":25,"category_name":29,"show_sort_weight":86,"slug":87},11,90,"presentations",{"id":89,"doc_module":22,"doc_module_name":25,"category_name":90,"show_sort_weight":91,"slug":92},12,"Resumes",80,"resumes",{"id":94,"doc_module":22,"doc_module_name":25,"category_name":95,"show_sort_weight":96,"slug":97},14,"Invoices",70,"invoices",{"id":99,"doc_module":22,"doc_module_name":25,"category_name":100,"show_sort_weight":101,"slug":102},15,"Posters",60,"posters",{"id":104,"doc_module":22,"doc_module_name":25,"category_name":105,"show_sort_weight":106,"slug":107},16,"Social Media",50,"social-media",{"id":109,"doc_module":22,"doc_module_name":25,"category_name":110,"show_sort_weight":111,"slug":112},17,"Forms",40,"forms",{"id":114,"doc_module":22,"doc_module_name":25,"category_name":115,"show_sort_weight":116,"slug":117},18,"Letters",30,"letters",{"id":119,"doc_module":22,"doc_module_name":25,"category_name":120,"show_sort_weight":121,"slug":122},21,"Paper Templates",5,"papers-templates",{"id":124,"doc_module":22,"doc_module_name":25,"category_name":125,"show_sort_weight":4,"slug":126},158,"General","general-158",{"code":4,"msg":82,"data":128},{"doc_id":79,"user_id":129,"nickname":42,"user_avatar":130,"doc_module":22,"category_id":85,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":131,"file_id":132,"file_url":133,"file_type":134,"file_size":135,"view_count":55,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":33,"language":136,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":137,"faqs":138,"seo_title":139,"seo_description":12,"update_tm":140,"read_time":26},549768702563,"https://ap-avatar.wpscdn.com/avatar/8000c4aa63b76e948b?x-image-process=image/resize,m_fixed,w_180,h_180&k=1786536092046926083","Bash Cheat Sheet  \nBy John Stowers  \nThis file contains short tables of commonly used items in this shell. In most cases the information applies to both the Bourne shell (sh) and the newer bash shell.  \nTests (for ifs and loops) are done with [] or with the test command.  \nChecking files:  \n-r file Check if file is readable.  \n-w file Check if file is writable.  \n-x file Check if we have execute access to file .  \n-f file Check if file is an ordinary file (as opposed to a directory, a device special file, etc . )  \n-s file Check if file has size greater than 0.  \n-d file Check if file is a directory .  \n-e file Check if file exists . Is true even if file is a directory .  \nExample:  \nif [ -s file ]  \nthen  \n\\#such and such  \nfi  \nChecking strings:  \ns1 = s2 Check if s1 equals s2 .  \ns1 != s2 Check if s1 is not equal to s2 .  \n-z s1 Check if s1 has size 0.  \n-n s1 Check if s2 has nonzero size .  \ns1 Check if s1 is not the empty string .  \nExample:  \nif [ $myvar = \"hello\" ] ; then echo \"We have a match\"  \nfi  \nChecking numbers:  \nNote that a shell variable could contain a string that represents a number. If you want to check the numerical value use one of the following:  \nn1 -eq n2 Check to see if n1 equals n2 .  \nn1 -ne n2 Check to see if n1 is not equal to n 2.  \nn1 -lt n2 Check to see if n1 \u003C n2 .  \nn1 -le n2 Check to see if n1 \u003C= n2 .  \nn1 -gt n2 Check to see if n1 > n2 .  \nn1 -ge n2 Check to see if n1 >= n2 .  \nExample:  \nif [ $\\# -gt 1 ] then  \necho \"ERROR: should have 0 or 1 command-line parameters\"  \nfi  \nBoolean operators:  \n! not  \n-a and  \n-o or  \nExample:  \nif [ $num -lt 10 -o $num -gt 100 ]  \nthen  \necho \"Number $num is out of range\"  \nelif [ ! -w $filename ]  \n[http://www.johnstowers.co.nz/blog/index.php/reference/bash-cheat-sheet/ 18/3/2553](http://www.johnstowers.co.nz/blog/index.php/reference/bash-cheat-sheet/ 18/3/2553)  \n» Bash Cheat Sheet Johns Blog Page 2 of 4  \nthen  \necho \"Cannot write to $filename\"  \nfi  \nNote that ifs can be nested. For example:  \nif [ $myvar = \"y\" ]  \nthen  \necho \"Enter count of number of items\"  \nread num  \nif [ $num -le 0 ]  \nthen  \necho \"Invalid count of $num was given\"  \nelse  \n\\#   do whatever ...  \nfi  \nfi  \nThe above example also illustrates the use of read to read a string from the keyboard and place it into a shell variable. Also note that most UNIX commands return a true (nonzero) or false (0) in the shell variable status to indicate whether they succeeded or not. This return value can be checked. At the command line echo $status. In a shell script use something like this:  \nif grep -q shell bshellref then  \necho else  \necho  \n\"true\"  \n\"false\"  \nfi  \nNote that-q is the quiet version of grep. It just checks whether it is true that the string shell occurs in the file bshellref. It does not print the matching lines like grep would otherwise do.  \nI/O Redirection:  \npgm > file Output of pgm is redirected to file.  \npgm \u003C file Program pgm reads its input from file.  \npgm >> file Output of pgm is appended to file.  \npgm1 | pgm2 Output of pgm1 is piped into pgm2 as the input to pgm2 .  \nn > file Output from stream with descriptor n redirected to file.  \nn >> file Output from stream with descriptor n appended to file.  \nn >& m Merge output from stream n with stream m.  \nn \u003C& m Merge input from stream n with stream m.  \n\u003C\u003C tag Standard input comes from here through next tag at start of line.  \nNote that file descriptor 0 is normally standard input, 1 is standard output, and 2 is standard error output.  \nShell Built-in Variables:  \n$0 Name of this shell script itself .  \n$1 Value of first command line parameter (similarly $2, $3, etc)  \n$\\# In a shell script, the number of command line parameters.  \n$* All of the command line parameters.  \n$ - Options given to the shell .  \n$? Return the exit status of the last command .  \n$$ Process id of script (really id of the shell running the script)  \nPattern Matching:  \n* Matches 0 or more characters.  \n? Matches 1 character .  \n[AaBbCc] Example","cbCaiakFbXltgvle","https://ap.wps.com/l/cbCaiakFbXltgvle","pdf",25079,"English","# Bash Cheat Sheet\n## Tests and Conditionals\n## File Checks, String Checks, and Number Checks\n## Boolean Operators and Nested if\n## Shell Built-in Variables\n## Pattern Matching and Quoting\n## Grouping, Case Statements, and Loops\n## I/O Redirection and Pipes","[{\"question\":\"How do you check whether a file is readable, writable, executable, or a directory in Bash?\",\"answer\":\"Use test expressions like [-r file] for readable, [-w file] for writable, [-x file] for execute access, [-d file] for a directory, and other flags such as -f for ordinary files and -e for existence.\"},{\"question\":\"What Bash operators are used to compare strings, numbers, and combine boolean conditions?\",\"answer\":\"Strings use equality/inequality such as [s1 = s2] and [s1 != s2], numbers use -eq/-ne/-lt/-le/-gt/-ge, and boolean combinations use !, -a for and, and -o for or.\"},{\"question\":\"How does Bash handle quoting and I/O redirection in the provided cheatsheet?\",\"answer\":\"Quoting rules include escaping with \\\\, command substitution with `cmd`, literal double quotes with \\\"...\\\" after interpreting $, `...`, and \\\\, and fully literal single quotes '...'. I/O redirection covers \\u003e, \\u003c, \\u003e\\u003e, pipelines with |, and descriptor-based redirection like n \\u003e file, n \\u003e\\u0026 m, and heredoc with \\u003c\\u003c tag.\"}]","Bash Cheat Sheet - reference - commands and syntax | PDF",1791924229]