{"id":11554,"date":"2024-04-03T20:23:19","date_gmt":"2024-04-03T18:23:19","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=11554"},"modified":"2024-04-03T20:23:24","modified_gmt":"2024-04-03T18:23:24","slug":"how-to-replace-string-in-bash-5-methods-linuxsimply","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2024\/04\/03\/how-to-replace-string-in-bash-5-methods-linuxsimply\/","title":{"rendered":"How to Replace String in Bash? [5 Methods] &#8211; LinuxSimply"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>In bash scripting, the replacement of string is important because it allows you to modify a variable and the text of a file. It also helps in file and text processing and to validate user input.<\/p>\n\n\n\n<p>To replace a string in bash, check the following methods:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Using Parameter expansion: <code class=\"\" data-line=\"\">${String\/pattern\/replacement}<\/code><\/li>\n\n\n\n<li>Using the sed command: <code class=\"\" data-line=\"\">sed &#039;s\/pattern\/replacement\/&#039; filename<\/code><\/li>\n\n\n\n<li>Using the awk command: <code class=\"\" data-line=\"\">&quot;input_str&quot; | awk &#039;pattern { action }&#039;<\/code><\/li>\n\n\n\n<li>Using the perl command: <code class=\"\" data-line=\"\">perl [options] -e &#039;action&#039; filename<\/code><\/li>\n\n\n\n<li>Using the tr command: <code class=\"\" data-line=\"\">tr &#039;old_chars&#039; &#039;new_chars&#039; &lt; &quot;input_file&quot;<\/code><\/li>\n<\/ol>\n\n\n\n<p>Dive into the article to learn these methods of how to replace a bash string in detail.<\/p>\n<cite>Source: <em><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-replace\/\">How to Replace String in Bash? [5 Methods] &#8211; LinuxSimply<\/a><\/em><\/cite><\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2024\/04\/03\/how-to-replace-string-in-bash-5-methods-linuxsimply\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"quote","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"federated","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[278],"tags":[298],"class_list":["post-11554","post","type-post","status-publish","format-quote","hentry","category-tricks","tag-shell","post_format-post-format-quote"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-30m","jetpack-related-posts":[{"id":1895,"url":"https:\/\/monodes.com\/predaelli\/2016\/11\/15\/50-most-frequently-used-unix-linux-commands-with-examples\/","url_meta":{"origin":11554,"position":0},"title":"50 Most Frequently Used UNIX \/ Linux Commands (With Examples)","author":"Paolo Redaelli","date":"2016-11-15","format":false,"excerpt":"Several pearl in 50 Most Frequently Used UNIX \/ Linux Commands (With Examples) Add line number for all non-empty-lines in a file $ sed '\/.\/=' thegeekstuff.txt | sed 'N; s\/\\n\/ \/' Remove duplicate lines using awk $ awk '!($0 in array) { array[$0]; print }' temp \u00a0 Print all lines\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7662,"url":"https:\/\/monodes.com\/predaelli\/2020\/10\/05\/advanced-unix-commands-to-boost-your-productivity\/","url_meta":{"origin":11554,"position":1},"title":"Advanced Unix commands to boost your productivity","author":"Paolo Redaelli","date":"2020-10-05","format":"status","excerpt":"Advanced Unix commands to boost your productivity The more you learn the better you get vi or better Vim find awk alias netstat Well, I can say that I do use them all except awk which I usually reimplement poorly using a combination of grep, sed and the like.","rel":"","context":"In &quot;Mood&quot;","block_context":{"text":"Mood","link":"https:\/\/monodes.com\/predaelli\/category\/mood\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11563,"url":"https:\/\/monodes.com\/predaelli\/2024\/04\/03\/3-ways-to-delete-all-files-in-a-directory-except-one-or-few-files-with-extensions\/","url_meta":{"origin":11554,"position":2},"title":"3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions","author":"Paolo Redaelli","date":"2024-04-03","format":"quote","excerpt":"The different extended pattern matching operators are listed below, where pattern-list is a list containing one or more filenames, separated using the | character: *(pattern-list) \u2013 matches zero or more occurrences of the specified patterns ?(pattern-list) \u2013 matches zero or one occurrence of the specified patterns +(pattern-list) \u2013 matches one\u2026","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10844,"url":"https:\/\/monodes.com\/predaelli\/2023\/10\/07\/how-to-use-the-bg-command-on-linux-pi-my-life-up\/","url_meta":{"origin":11554,"position":3},"title":"How to use the bg Command on Linux &#8211; Pi My Life Up","author":"Paolo Redaelli","date":"2023-10-07","format":false,"excerpt":"Well, I learned a couple of details I didn't knew from How to use the bg Command on Linux - Pi My Life Up How to use the bg Command on Linux by Emmet Published Feb 06, 2022 Linux Commands \u00a0 In this guide, we will be showing you how\u2026","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"Emmet Avatar","src":"https:\/\/i0.wp.com\/pimylifeup.com\/wp-content\/uploads\/2022\/04\/pimylifeup_editor_avatar.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":9031,"url":"https:\/\/monodes.com\/predaelli\/2021\/12\/30\/packaging-pyqt5-apps-with-fbs-distribute-cross-platform-gui-applications-with-the-fman-build-system\/","url_meta":{"origin":11554,"position":4},"title":"Packaging PyQt5 apps with fbs \u2014 Distribute cross-platform GUI applications with the fman Build System","author":"Paolo Redaelli","date":"2021-12-30","format":"link","excerpt":"Packaging PyQt5 apps with fbs \u2014 Distribute cross-platform GUI applications with the fman Build System Packaging PyQt5 apps with fbs Distribute cross-platform GUI applications with the fman Build System by Martin Fitzpatrick Read time 13:29 Packaging and distribution Packaging and distribution course Packaging PyQt5 applications for Windows, with PyInstaller Packaging\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3766,"url":"https:\/\/monodes.com\/predaelli\/2018\/02\/08\/how-can-i-escape-white-space-in-a-bash-loop-list-stack-overflow\/","url_meta":{"origin":11554,"position":5},"title":"How can I escape white space in a bash loop list? &#8211; Stack Overflow","author":"Paolo Redaelli","date":"2018-02-08","format":"link","excerpt":"I find this nice and elegant. The other proposed solutions may handle files with newline in their name, but currently I see such file names as nonsense find . -type d | while read file; do echo $file; done However, doesn't work if the file-name contains newlines. The above is\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/11554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=11554"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/11554\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=11554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=11554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=11554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}