{"id":13683712,"url":"https://github.com/niieani/bashscript","last_synced_at":"2025-03-16T13:31:46.158Z","repository":{"id":40392296,"uuid":"131497145","full_name":"niieani/bashscript","owner":"niieani","description":"TypeScript to bash transpiler. Because.","archived":false,"fork":false,"pushed_at":"2023-12-15T14:37:28.000Z","size":33067,"stargazers_count":58,"open_issues_count":29,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-07T16:55:10.426Z","etag":null,"topics":["bash","hacktoberfest","transpiler","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/niieani.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-04-29T13:34:41.000Z","updated_at":"2025-02-24T12:09:13.000Z","dependencies_parsed_at":"2024-01-14T17:17:31.180Z","dependency_job_id":"b6cbb643-0fbb-47e8-b505-813d56157476","html_url":"https://github.com/niieani/bashscript","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niieani%2Fbashscript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niieani%2Fbashscript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niieani%2Fbashscript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niieani%2Fbashscript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/niieani","download_url":"https://codeload.github.com/niieani/bashscript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243818171,"owners_count":20352629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","hacktoberfest","transpiler","typescript"],"created_at":"2024-08-02T13:02:25.483Z","updated_at":"2025-03-16T13:31:42.863Z","avatar_url":"https://github.com/niieani.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# BashScript\n\nA JavaScript/TypeScript to `bash` transpiler. **Work in progress.**\n\nWhy? Mainly because I wanted to learn how to make a transpiler.\n\nI also wanted to experiment with `bash` and how far we could stretch\nthis old, yet widely cross-platform language.\n\nI've previously created a framework trying to make `bash` as usable as possible:\n[Bash Infinity](https://github.com/niieani/bash-oo-framework).\nThis seemed like the natural next step.\n\nAlso, because... why not? 🤓\n\n## REPL\n\nNot much works, but it's cool. :-)\n\nFind it here: **[REPL](https://niieani.github.io/bashscript/)**.\n\n## Specification (WIP)\n\n### Function invocation\n\nFunction calls are transpilled as calls to bash functions/commands\ne.g.\n\n#### input\n\n```typescript\necho('hi')\n```\n\n#### output\n\n```bash\necho hi\n```\n\nif the call is used in the position of parameter or assignment:\ne.g.\n\n#### input\n\n```typescript\nconst out = concat('hi', 'ho')\n```\n\n#### output\n\n```bash\ndeclare out=$(concat 'hi' 'ho')\n```\n\n### Function declaration\n\n#### input\n\n```typescript\nfunction concat(a, b) {\n  return `${a}${b}`\n}\n```\n\n#### output\n\n```bash\nfunction concat {\n  local a=\"${1}\"\n  local b=\"${2}\"\n  echo \"${a}${b}\"\n}\n```\n\n### Invoking properties\n\n#### input\n\n```typescript\nconst a = 'abc'\nconst b = a.toLowercase()\n```\n\n#### output\n\n```bash\ndeclare a='abc'\ndeclare b=\"$(__callProperty a toLowercase)\"\n```\n\n### Invoking properties with parameters\n\n#### input\n\n```js\nconst arr = ['abc']\narr.push('xyz')\n```\n\n#### output\n\n```bash\ndeclare arr=('abc')\n__callProperty arr push 'xyz'\n```\n\n### Operators\n\n#### input\n\n```typescript\nconst a = 'abc' + 'xyz'\n```\n\n#### output\n\n```bash\ndeclare a=\"$(__operator_addition 'abc' 'zyx')\"\n```\n\n#### output helpers\n```bash\n# e.g.\n__callProperty() {\n  if isArray arr\n  then\n    if property === 'push'\n    then\n      arr+=(\"${arr[@]}\")\n    fi\n  fi\n}\n```\n\n### Lambda functions\n\n#### input\n\n```js\nconst concat = (a) =\u003e {\n  const c = `${a}-super`\n  return (b) =\u003e {\n    return `${c}${b}`\n  }\n}\nconst withOne = concat('one')\nconst result = withOne('two')\n```\n\n#### output\n\n```bash\nfunction concat {\n  # params:\n  local a=\"${1}\"\n  # function body:\n  local c=\"${a}-super\"\n  # preapplied function:\n  # lambda declaration is:\n  # [type, name, scoped_declarations_to_eval]\n  local -a declaration=(\n    function\n    __lambda_concat_1\n    \"$(declare -p c)\"\n  )\n  echo \"$(declare -p declaration)\"\n}\n\n# all functions are top level\nfunction __lambda_concat_1 {\n  # scoped variables:\n  eval \"${1}\"; shift;\n  # actual function body:\n  local b=\"${1}\"\n  echo \"${c}${b}\"\n}\n\nfunction __callVar {\n  # TODO: add check if var is a declaration\n  eval \"${!1}\"; shift;\n  if [[ \"${declaration[1]}\" == \"function\" ]]\n  then\n    \"${declaration[1]}\" \"${declaration[2]}\" \"$@\"\n  fi\n}\n\ndeclare withOne=\"$(concat 'one')\"\ndeclare result=\"$(__callVar withOne 'two')\"\n```\n\n### Lambdas with scoped variables\n\n#### input\n\n```typescript\nconst arr = [1, 2, 3]\nconst result = arr\n  .map(num =\u003e num + 1)\n  .map(num =\u003e num - 1)\n```\n\n#### output\n\n```bash\ndeclare declaration=(1 2 3)\ndeclare arr=\"$(declare -p declaration)\"\nunset declaration\n\n__lamda_arr_map_anon_1() {\n  # scoped variables (if any):\n  eval \"${1}\"; shift;\n  # actual function body:\n  local num=\"${1}\"\n  echo \"$(__operator_addition num 1)\"\n}\n__lamda_arr_map_anon_2() {\n  # scoped variables (if any):\n  eval \"${1}\"; shift;\n  # actual function body:\n  local num=\"${1}\"\n  echo \"$(__operator_substraction num 1)\"\n}\n\ndeclare _result1=\"$(__callProperty arr map __lamda_arr_map_anon_1)\"\ndeclare result=\"$(__callProperty _result1 map __lamda_arr_map_anon_2)\"\nunset _result1\n```\n\n\n### Objects and other literals\n\n#### input\n\n```typescript\nconst outerObject = {a: 'boom'}\nconst outerHello = 'hello from outer space!'\nconst obj = {\n  a: {\n    aa: 123,\n    bbb: {c: ['inner1', 'inner2', 'inner3']}\n  },\n  b: 'hello',\n  c: outerObject,\n  d: outerHello,\n}\n\necho(obj.a.aa)\n```\n\n#### output\n\n```shell script\n#!/usr/bin/env bash\ndeclare -A outerObject=(\n  [__type]=object\n\n  [a]=\"boom\"\n)\n\ndeclare outerHello='hello from outer space!'\n\ndeclare -a __obj_a_bbb_c=(inner1 inner2 inner3)\ndeclare -A __obj_a_bbb=([ref_c]=__obj_a_bbb_c [__type]=object)\ndeclare -A __obj_a=([aa]=123 [ref_bbb]=\"__obj_a_bbb\" [__type]=object)\ndeclare -A obj=(\n  [__type]=object\n\n  [ref_a]=\"__obj_a\"\n  [b]=\"hello\"\n  [ref_c]=\"outerObject\"\n  # we need the TypeScript type to tell whether this is a reference or a plain object\n  [d]=\"${outerHello}\"\n)\n\n# perhaps instead of using the ref_ prefix, we should use an uncommon prefix in the value\n# that way we can reuse it for other reference-related functionality\n\n@objectProperty() {\n  local objName=\"$1\"\n  local property=\"$2\"\n  shift\n  shift\n  local refName=\"${objName}[\\\"ref_${property}\\\"]\"\n  local typeName=\"${objName}[\\\"__type\\\"]\"\n  # we need to check type, because bash will return the value of first property if it doesn't exist on the object 🤦\n  if [[ -v \"${refName}\" \u0026\u0026 \"${!typeName}\" == 'object' ]]; then\n    local value=\"${!refName}\"\n    if [[ \"${#}\" -gt 0 ]]; then\n      @objectProperty \"${value}\" \"$@\"\n      return\n    fi\n    echo \"__ref:${value}\"\n  else\n    refName=\"${objName}[${property}]\"\n    if [[ -v \"${refName}\" ]]; then\n      if [[ \"${#}\" -gt 0 ]]; then\n        echo \"Error: Cannot read property '${1}' of a non-object '${property}'.\"\n        return\n      fi\n      echo \"${!refName}\"\n    else\n      echo \"Error: Cannot read property '${property}' of '${objName}'.\"\n    fi\n  fi\n}\n\n@objectProperty obj a aa\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniieani%2Fbashscript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniieani%2Fbashscript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniieani%2Fbashscript/lists"}