{"id":17038186,"url":"https://github.com/bchao1/shell-scripting-cheatsheet","last_synced_at":"2026-04-29T22:04:28.026Z","repository":{"id":113819820,"uuid":"146967184","full_name":"bchao1/shell-scripting-cheatsheet","owner":"bchao1","description":null,"archived":false,"fork":false,"pushed_at":"2018-09-24T08:17:59.000Z","size":31,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-09T14:24:39.145Z","etag":null,"topics":["bash","shell","shell-script"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bchao1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-01T04:23:46.000Z","updated_at":"2024-12-06T13:54:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"63b3b1e6-9e33-427e-b35f-b327e0229c95","html_url":"https://github.com/bchao1/shell-scripting-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bchao1/shell-scripting-cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bchao1%2Fshell-scripting-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bchao1%2Fshell-scripting-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bchao1%2Fshell-scripting-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bchao1%2Fshell-scripting-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bchao1","download_url":"https://codeload.github.com/bchao1/shell-scripting-cheatsheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bchao1%2Fshell-scripting-cheatsheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32445585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T20:22:27.477Z","status":"ssl_error","status_checked_at":"2026-04-29T20:22:26.507Z","response_time":110,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","shell","shell-script"],"created_at":"2024-10-14T08:56:13.480Z","updated_at":"2026-04-29T22:04:27.992Z","avatar_url":"https://github.com/bchao1.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shell-Scripting-CheatSheet\n\n## Table of Contents \n- First Script\n- Basic Printing\n- Variables\n- Loops\n- Tests\n- Special character usage: `$`, `*`, and etc.\n\n## First Script\nSince there are various kinds of shells in a Unix system, we usually define the type of shell we want to use in our shell script before we actually get into the main content. In the first line of your script, type\n```\n#!./bin/sh\n```\nUsually the shell is located in the `./bin` directory, the `!#` is followed by the path to the shell we wish to use. In this case, we are using the **Bourne shell**. We can also use **bash**, which is short for **Bourne-Again shell**, by typing\n```\n#!./bin/bash\n```\nAfter we have configured the shell to run our script, we can start writing our first shell script. To print something one the terminal, we use the command `echo`. In your script, type\n```\necho Hello World\n```\nTo run the shell script, simply type `./\u003cname of the script\u003e` in your terminal. In my case, I named by shell script `first_script.sh`.\n```\n$ ./first_script.sh\n```\nIn the terminal, you should see \n```\n$ ./first_script.sh\nHello World\n$\n```\nYou have now written your first shell script!\n\n## Basic Printing\nAs seen in the example above, the `echo` command is to print something on the terminal. It is essentially the same as `print` in **Python** (since Python is also a scripting language, it is obvious to make this analogy). The content following `echo`, or any other shell commands, are called **arguments**. \n```\n$ echo Hello World\nHello World\n$ echo Hello     World\nHello World\n```\nBefore the shell executes a line, it first *parses* the line. The first token in the line is the *command*, and the following tokens, seperated by whitespaces, are the arguments. In both examples above, `echo` is the command (print something), and the arguments are `Hello` and `World`. `echo` prints the arguments seperated by a space, hence the result.\n\nTo preserve the whitespaces between the two words, we enclose the string we wish to print with `\"`.\n```\n$ echo \"Hello     World\"\nHello     World\n```\n`echo` now treats `\"Hello     World\"` as a single argument.\n\n## Variables \nLike any other programming language, we can also assign values to **variables** in shell scripts. Consider the following command\n```\n$ var=10\n```\nThe value `10` is **assigned** to the **variable** `var`. \nNote that no spaces should appear around `=`. For the line \n```\n$ var = 10\n```\nThe shell would treat `var` as a command and `=`, `10` as arguments. Apparently there are no shell commands named `var`, hence you would recieve an error message\n```\n$ var = 10\nvar: no command found\n```\nPrinting the value of the variable is similar to printing a string, except we add a `$` in front of the variable.\n```\n$ echo \"The value of var is $var\"\nThe value of var is 10\n```\nThe command `read` assigns user input to a variable. Type the following in your shell script, then run the script on the terminal.\n``` \necho \"What is your name?\"\nread USERNAME\necho \"Hello $USERNAME\n```\nYou should see the following\n```\n$ ./test.sh\nWhat is your name? \n```\nEnter your name and see\n```\n$ ./test.sh\nWhat is your name? Brian\nHello Brian\n```\n### Variable Scopes\n\n## Loops\nMost shells support the `while` and `for` loop, just like any other programming language.\n### `for` loops\n`for` loops itereate through a list of values. Try to run the following script on your terminal\n```\nfor NUMBER in 1 2 3 4 5\ndo \n  echo \"NUMBER is $NUMBER\"\ndone\n```\nYou should see\n```\n$ ./test.sh\nNUMBER is 1\nNUMBER is 2\nNUMBER is 3\nNUMBER is 4\nNUMBER is 5\n```\nIn each iteration, the variable (should be defined between `for` and `in`) will be assigned to one of the arguments in the argument list (located after `in`). \nIt is recommended to indent the content between `do` and `done` for clarity.\n\n### `while` loops\nThe syntax of a basic while loop is as follows \n```\nwhile [ test ]\ndo\n  ...\ndone\n```\n`[test]` is the **condition** that the loop checks every iteration. **Tests** will be introduced in the next chapter. For now, we try to implement an infinite loop, where the condition is replaced by a `*`, indicating *true* for every iteration.\n```\nwhile *\ndo \n  echo \"This is an infinite loop!\"\ndone\n```\n\n### Tests `[   ]`\n**Conditional statements** are essential elements in any programming language. In shell scripts, conditional statements are enclosed by two middle brackets `[   ]`; for example:\n```\nvar=100 # var is assigned to 100\nif [ var = 100 ] # checks if var equals to 100\nthen \n  echo \"var is 100\"\nelse\n  echo \"var is not 100\"\nfi\n```\nNote that in test statements, brackets must be surrounded by spaces. That is, \n```\nif' '[' 'var' '=' '100' ']\n```\nis the **only** correct format for test statements. Note that `=` here refers to equality, and needs to be surrounded by spaces too, which is different from assignment.\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbchao1%2Fshell-scripting-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbchao1%2Fshell-scripting-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbchao1%2Fshell-scripting-cheatsheet/lists"}