{"id":13863042,"url":"https://github.com/mdminhazulhaque/foreach","last_synced_at":"2025-05-07T13:34:00.460Z","repository":{"id":149138057,"uuid":"68639545","full_name":"mdminhazulhaque/foreach","owner":"mdminhazulhaque","description":":repeat: Read lines from file/stdin and execute them as CLI argument","archived":false,"fork":false,"pushed_at":"2018-05-20T12:10:11.000Z","size":3,"stargazers_count":16,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-05T06:06:37.146Z","etag":null,"topics":["bash","foreach","iterator","line","stdin"],"latest_commit_sha":null,"homepage":"","language":"Shell","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/mdminhazulhaque.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}},"created_at":"2016-09-19T19:35:04.000Z","updated_at":"2024-08-05T06:06:37.146Z","dependencies_parsed_at":null,"dependency_job_id":"9297ed18-b21c-45c2-8039-ca726cac5886","html_url":"https://github.com/mdminhazulhaque/foreach","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/mdminhazulhaque%2Fforeach","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdminhazulhaque%2Fforeach/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdminhazulhaque%2Fforeach/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdminhazulhaque%2Fforeach/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdminhazulhaque","download_url":"https://codeload.github.com/mdminhazulhaque/foreach/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224608944,"owners_count":17339785,"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","foreach","iterator","line","stdin"],"created_at":"2024-08-05T06:02:00.475Z","updated_at":"2024-11-14T11:12:49.222Z","avatar_url":"https://github.com/mdminhazulhaque.png","language":"Shell","funding_links":[],"categories":["Shell"],"sub_categories":[],"readme":"# foreach\n\n`foreach` is a simple bash script which reads given file/stdin line by line and executes specific command with the line as argument.\n\n## dude! ever heard of xargs?\n\nYup I did! But I am too lazy to memorize its complex argument passing techniques. So I wrote my own script.\n\nHere is a simple example.\n\n```bash\n~:minhaz $ cat links.txt \nhttps://www.youtube.com/watch?v=el0-J3tJs5M\nhttps://www.youtube.com/watch?v=CBsXggn_4qA\nhttps://www.youtube.com/watch?v=ZoCyVrl94yE\n```\n\nNow you want to execute `youtube-dl` with each of the line as argument. `xargs` might help in this case.\n\n```bash\n~:minhaz $ cat links.txt | xargs -L1 echo youtube-dl\nyoutube-dl https://www.youtube.com/watch?v=el0-J3tJs5M\nyoutube-dl https://www.youtube.com/watch?v=CBsXggn_4qA\nyoutube-dl https://www.youtube.com/watch?v=ZoCyVrl94yE\n```\n\nBut wait! `xargs`? `-L1`? That's too much to remember. And what if I want to use the argument in the middle of some commands? Like the following?\n\n```bash\n~:minhaz $ cat bands.txt \ncoldplay\ndream theater\niron maiden\nmetallica\npink floyd\n\n~:minhaz $ cat bands.txt | somemagiccommand echo i love {} and {} rocks\ni love coldplay and coldplay rocks\ni love dream theater and dream theater rocks\ni love iron maiden and iron maiden rocks\ni love metallica and metallica rocks\ni love pink floyd and pink floyd rocks\n```\n\n`foreach` is that `somemagiccommand`. It does format the line from a file where referenced as `{}`. You can use `{}` as much as you want, wherever the position of the argument is.\n\n```bash\n~:minhaz $ cat distro.txt \nkubuntu\ndebian\narch\nfreebsd\nopensuse\n\n~:minhaz $ foreach distro.txt echo {} is awesome # starts with {}\nkubuntu is awesome\ndebian is awesome\narch is awesome\nfreebsd is awesome\nopensuse is awesome\n```\n\n`foreach` can also be used without piping data into it. In such cases first argument will be taken as input filename. For example,\n\n```bash\n~:minhaz $ cat fruits.txt \napple\nbanana\ncherry\n\n~:minhaz $ foreach fruits.txt echo i love {} # ends with {}\ni love apple\ni love banana\ni love cherry\n```\n\nYou can skip `{}` if it will be passed as only one argument of the command you want to run. For example,\n\n```bash\n~:minhaz $ cat links.txt \nhttps://www.youtube.com/watch?v=el0-J3tJs5M\nhttps://www.youtube.com/watch?v=CBsXggn_4qA\nhttps://www.youtube.com/watch?v=ZoCyVrl94yE\n\n~:minhaz $ foreach links.txt youtube-dl # {} is not used here\n[youtube] el0-J3tJs5M: Downloading ...\n[youtube] CBsXggn_4qA: Downloading ...\n[youtube] ZoCyVrl94yE: Downloading ...\n```\n\n## Installation\n\nDownload `foreach` and put anywhere in your `PATH`. Or do the following.\n\n```bash\nsudo wget -O /usr/local/bin/foreach https://raw.githubusercontent.com/mdminhazulhaque/foreach/master/foreach\nsudo chmod +x /usr/local/bin/foreach\n```\n\n## Changelogs\n\n- [x] 2017-05-05 Fixed an issue where last line was not being read\n\n\n## More Bugs?\n\nOpen issues if you find any. You are also welcome to submit patches.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdminhazulhaque%2Fforeach","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdminhazulhaque%2Fforeach","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdminhazulhaque%2Fforeach/lists"}