{"id":20531896,"url":"https://github.com/ad5001/bashoop","last_synced_at":"2026-04-21T04:32:59.290Z","repository":{"id":71031291,"uuid":"442578769","full_name":"Ad5001/BashOOP","owner":"Ad5001","description":"Simple OOP implementation for bash.","archived":false,"fork":false,"pushed_at":"2022-01-07T14:30:36.000Z","size":37,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-06T02:46:48.077Z","etag":null,"topics":["bash","oop"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ad5001.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2021-12-28T20:41:49.000Z","updated_at":"2023-01-08T13:21:50.000Z","dependencies_parsed_at":"2023-08-28T18:49:24.607Z","dependency_job_id":null,"html_url":"https://github.com/Ad5001/BashOOP","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Ad5001/BashOOP","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ad5001%2FBashOOP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ad5001%2FBashOOP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ad5001%2FBashOOP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ad5001%2FBashOOP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ad5001","download_url":"https://codeload.github.com/Ad5001/BashOOP/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ad5001%2FBashOOP/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32076975,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-21T02:38:07.213Z","status":"ssl_error","status_checked_at":"2026-04-21T02:38:06.559Z","response_time":128,"last_error":"SSL_read: 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","oop"],"created_at":"2024-11-16T00:10:43.151Z","updated_at":"2026-04-21T04:32:59.269Z","avatar_url":"https://github.com/Ad5001.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BashOOP\n\nThis idea was inspired from https://stackoverflow.com/a/40981277 in order to be adapted into making a proper OOP with constructors, properties and namespace system for bash.\n\n## Reference:\n\n- .shn: **SH**ell **N**amespace\n- .shc: **SH**ell **C**lass\n\n## Syntax:\n\n\nTo declare objects, there exists 3 scripts.\n\n- The main script in which we'll use our object (suggested extension: `.sh`)\n- The namespace declaration script (suggested extension: `.shn`)\n- The class declaration script (suggested extension: `.shc`)\n\nThe full example is available in the \"example\" directory.\n\n### Declaring objects.\n\nAn object has a type name, properties and functions.    \nTo declare a property, you can use the `property` function. For example, for an class named `Object`, you can declare a property name using:\n```bash\nproperty Object.name\n```\n**NOTE**: Bash doesn't have a typing system, so you cannot set property types.\nYou can also set default values by adding a property after the declaration, e.g:\n```bash\nproperty Object.name \"Example\"\n```\n\nClass functions are declared the same way you would in bash, except it uses a prefix with object type. For example:\n```bash\nObject.print() {\n    echo \"Example OOP from $(this.name)!\"\n}\n```\nAs you can see here, you can access properties of the object using the `this` keyword in a function call.    \nSimilarly, you can set properties using a `=` and value argument. For example:\n```bash\nthis.name = \"New name\"\n```\n\nObjects can also have constructors which will be called at the creation of the object with arguments provided at the creation.   \nThey are simply a function with the name `constructor`. They aren't mandatory for any object.\n\n### Creating a namespace.\nWhile you can import objects directly in the global namespace, it's recommanded to use a separate namespace file.\n\nWhen you've created your namespace file, you can specify the name of the namespace using the `namespace` keyword:\n```bash\nnamespace Example\n```\nYou can then declare object classes using the `class` directive by specifying it's name and associated script file. For example:\n```bash\nclass Object \"Object.shc\"\n```\n\nAll objects created under this class will be accessible with namespace as prefix (here our Object class would be accessible under `Example.Object`).\n\nSimilarly, static classes can be declared using the `static_class` keyword.\n```bash\nstatic_class Static \"Static.shc\"\n```\n\n**NOTE**: Static classes can't have properties. However, you can declare \"namespace properties\" using first the `createPropertyHolder` and then declare properties directly in the namespace file like this:\n```bash\ncreatePropertyHolder Example\n\nproperty Example.name\n```\n\n### Using objects.\nNow that we've created our namespace, we will want to use it and our objects in our script.   \nFirst things first, we'll want to import the library `oop.sh`. Depending on where it's located, you will want to use a global variable indicating it's location.    \n```bash\n. $OOP_ROOT/oop.sh # Import library.\n```\n\nAfter that, we'll want to import our namespace file with all it's classes prefixed in the namespace name.\n```bash\nimportNamespace \"Example.shn\"\n```\n\nAfter that you can declare the object using the following syntax: `\u003cObjectType\u003e \u003cvariableName\u003e [constructor arguments...]`. For example:\n```bash\nExample.Object obj1 \"Test\"\n```\n\nYou can then call it's functions.\n```bash\n$obj1.print\n```\n\n... or access and edit it's properties.\n```bash\nname=$($obj1.name)\n$obj1.name = \"New name\"\n```\n\nYou can store objects in variables as a string. For example, you can have have objects as class arguments, function returs or arrays of objects like this:\n```bash\nExample.Object obj1 \"First Object\"\nExample.Object obj2 \"Second Object\"\nobjs=($obj1 $obj2)\n${objs[0]}.print\n${objs[1]}.print\n```\n\nYou can also access the static classes by using their class type directly. For example:\n```bash\nExample.Static.print \"Example text\"\n```\n\nIf you find that using the namespace everytime is a bit cumbersome, you can use the `using` keyword to alias all classes of a namespace into the global namespace.    \nExample usage:\n```bash\nusing Example\n\nObject usingObj \"New\"\n\n$usingObj.print\n```\n**NOTE**: When `using` a namespace which contains static classes, please note that the static class file will be re-imported.\n**NOTE**: `using` is not file contextual. So `using` a namespace will use it in every bash script.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fad5001%2Fbashoop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fad5001%2Fbashoop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fad5001%2Fbashoop/lists"}