{"id":85892,"date":"2025-04-21T09:16:06","date_gmt":"2025-04-21T02:16:06","guid":{"rendered":"https:\/\/itviecblog.uptech.vn\/?p=85892"},"modified":"2025-06-17T09:46:23","modified_gmt":"2025-06-17T02:46:23","slug":"bash-script-la-gi","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/","title":{"rendered":"Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Bash_script_la_gi\" >Bash script l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Uu_%E2%80%93_nhuoc_diem_cua_Bash_Script\" >\u01afu &#8211; nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a Bash Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Bash_script_hoat_dong_nhu_the_nao\" >Bash script ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Mot_so_khai_niem_co_ban_cua_Bash_script\" >M\u1ed9t s\u1ed1 kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n c\u1ee7a Bash script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Chay_Bash_script_nhu_the_nao\" >Ch\u1ea1y Bash script nh\u01b0 th\u1ebf n\u00e0o?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Cac_phuong_phap_de_su_dung_Bash_script_hieu_qua_hon\" >C\u00e1c ph\u01b0\u01a1ng ph\u00e1p \u0111\u1ec3 s\u1eed d\u1ee5ng Bash script hi\u1ec7u qu\u1ea3 h\u01a1n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Cau_hoi_thuong_gap_ve_Bash_script\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 Bash script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>Bash script l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng hi\u1ec7u qu\u1ea3, th\u00f4ng qua c\u00fa ph\u00e1p d\u1ec5 hi\u1ec3u v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng cao. Khi hi\u1ec3u th\u00e0nh th\u1ea1o Bash script, b\u1ea1n c\u00f3 th\u1ec3 x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p nhanh ch\u00f3ng v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n.\u00a0<\/strong><\/em><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bash script l\u00e0 g\u00ec? \u01afu v\u00e0 nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a n\u00f3?<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1ch Bash script ho\u1ea1t \u0111\u1ed9ng<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n c\u1ee7a Bash script<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Bash_script_la_gi\"><\/span><b>Bash script l\u00e0 g\u00ec?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Bash script l\u00e0 m\u1ed9t t\u1ec7p v\u0103n b\u1ea3n ch\u1ee9a c\u00e1c l\u1ec7nh m\u00e0 b\u1ea1n mu\u1ed1n m\u00e1y t\u00ednh th\u1ef1c hi\u1ec7n t\u1ef1 \u0111\u1ed9ng. N\u00f3 gi\u1ed1ng nh\u01b0 m\u1ed9t k\u1ecbch b\u1ea3n phim, nh\u01b0ng l\u00e0 d\u00e0nh cho m\u00e1y t\u00ednh.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c l\u1ec7nh trong Bash script th\u01b0\u1eddng l\u00e0 nh\u1eefng l\u1ec7nh m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 g\u00f5 tr\u1ef1c ti\u1ebfp tr\u00ean d\u00f2ng l\u1ec7nh nh\u01b0 <\/span><span style=\"font-weight: 400;\">ls<\/span><span style=\"font-weight: 400;\"> (li\u1ec7t k\u00ea th\u01b0 m\u1ee5c) hay <\/span><span style=\"font-weight: 400;\">cp<\/span><span style=\"font-weight: 400;\"> (sao ch\u00e9p t\u1ec7p). Ngo\u00e0i ra, trong Bash script, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 v\u00e0 c\u00fa ph\u00e1p m\u1ea1nh m\u1ebd nh\u01b0 v\u00f2ng l\u1eb7p, c\u00e1c h\u00e0m \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a nh\u1eefng c\u00f4ng vi\u1ec7c ph\u1ee9c t\u1ea1p m\u00e0 n\u1ebfu l\u00e0m th\u1ee7 c\u00f4ng tr\u00ean d\u00f2ng l\u1ec7nh, b\u1ea1n s\u1ebd ph\u1ea3i nh\u1eadp nhi\u1ec1u l\u1ec7nh kh\u00e1c nhau.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1ed9t \u0111i\u1ec1u th\u00fa v\u1ecb l\u00e0 m\u1ecdi l\u1ec7nh b\u1ea1n c\u00f3 th\u1ec3 ch\u1ea1y tr\u00ean d\u00f2ng l\u1ec7nh \u0111\u1ec1u c\u00f3 th\u1ec3 \u0111\u01b0a v\u00e0o Bash script, v\u00e0 n\u00f3 s\u1ebd ho\u1ea1t \u0111\u1ed9ng ch\u00ednh x\u00e1c nh\u01b0 khi b\u1ea1n g\u00f5 th\u1ee7 c\u00f4ng. B\u1ea1n kh\u00f4ng c\u1ea7n ph\u1ea3i thay \u0111\u1ed5i c\u00e1ch vi\u1ebft l\u1ec7nh, ch\u1ec9 c\u1ea7n nh\u1eadp ch\u00fang v\u00e0o t\u1ec7p v\u0103n b\u1ea3n v\u00e0 ch\u1ea1y t\u1ec7p \u0111\u00f3. N\u1ebfu b\u1ea1n \u0111\u00e3 quen thu\u1ed9c v\u1edbi d\u00f2ng l\u1ec7nh, th\u00ec vi\u1ebft Bash script s\u1ebd r\u1ea5t d\u1ec5 d\u00e0ng v\u1edbi b\u1ea1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Th\u01b0\u1eddng th\u00ec c\u00e1c t\u1ec7p Bash script s\u1ebd c\u00f3 ph\u1ea7n m\u1edf r\u1ed9ng <\/span><span style=\"font-weight: 400;\">.sh<\/span><span style=\"font-weight: 400;\">, m\u1eb7c d\u00f9 h\u1ec7 th\u1ed1ng Linux kh\u00f4ng y\u00eau c\u1ea7u ph\u1ea3i c\u00f3 ph\u1ea7n m\u1edf r\u1ed9ng n\u00e0y \u0111\u1ec3 script c\u00f3 th\u1ec3 ch\u1ea1y.<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"\/blog\/bash-shell-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Bash Shell l\u00e0 g\u00ec? C\u00e1c t\u00ednh n\u0103ng ch\u00ednh c\u1ee7a Bash Shell<\/strong><\/a><\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Uu_%E2%80%93_nhuoc_diem_cua_Bash_Script\"><\/span><b>\u01afu &#8211; nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a Bash Script<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>\u01afu \u0111i\u1ec3m c\u1ee7a Bash Script l\u00e0 g\u00ec?<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00f3 kh\u1ea3 n\u0103ng t\u1ef1 \u0111\u1ed9ng h\u00f3a<\/b><span style=\"font-weight: 400;\">: Bash script gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 l\u1eb7p \u0111i l\u1eb7p l\u1ea1i nh\u01b0 sao l\u01b0u, qu\u1ea3n l\u00fd t\u1ec7p tin hay c\u00e0i \u0111\u1eb7t ph\u1ea7n m\u1ec1m, gi\u00fap ti\u1ebft ki\u1ec7m th\u1eddi gian v\u00e0 gi\u1ea3m thi\u1ec3u sai s\u00f3t khi th\u1ef1c hi\u1ec7n th\u1ee7 c\u00f4ng.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ednh di \u0111\u1ed9ng cao<\/b><span style=\"font-weight: 400;\">: Bash script c\u00f3 th\u1ec3 ch\u1ea1y tr\u00ean nhi\u1ec1u n\u1ec1n t\u1ea3ng kh\u00e1c nhau nh\u01b0 Unix, Linux, macOS v\u00e0 th\u1eadm ch\u00ed tr\u00ean Windows n\u1ebfu s\u1eed d\u1ee5ng ph\u1ea7n m\u1ec1m gi\u1ea3 l\u1eadp ho\u1eb7c m\u00e1y \u1ea3o. \u0110i\u1ec1u n\u00e0y gi\u00fap Bash script r\u1ea5t linh ho\u1ea1t v\u00e0 d\u1ec5 s\u1eed d\u1ee5ng tr\u00ean nhi\u1ec1u h\u1ec7 \u0111i\u1ec1u h\u00e0nh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ednh linh ho\u1ea1t cao<\/b><span style=\"font-weight: 400;\">: B\u1ea1n c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng ch\u1ec9nh s\u1eeda v\u00e0 \u0111i\u1ec1u ch\u1ec9nh Bash script \u0111\u1ec3 ph\u00f9 h\u1ee3p v\u1edbi nhu c\u1ea7u c\u1ee5 th\u1ec3. H\u01a1n n\u1eefa, ch\u00fang c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c k\u1ebft h\u1ee3p v\u1edbi c\u00e1c ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh kh\u00e1c ho\u1eb7c c\u00e1c c\u00f4ng c\u1ee5 b\u1ed5 sung \u0111\u1ec3 t\u1ea1o ra nh\u1eefng script m\u1ea1nh m\u1ebd h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>D\u1ec5 ti\u1ebfp c\u1eadn<\/b><span style=\"font-weight: 400;\">: Vi\u1ebft Bash script r\u1ea5t \u0111\u01a1n gi\u1ea3n v\u00e0 kh\u00f4ng c\u1ea7n c\u00e1c c\u00f4ng c\u1ee5 ph\u1ee9c t\u1ea1p. B\u1ea1n ch\u1ec9 c\u1ea7n m\u1ed9t tr\u00ecnh so\u1ea1n th\u1ea3o v\u0103n b\u1ea3n c\u01a1 b\u1ea3n v\u00e0 shell interpreter \u0111\u00e3 c\u00f3 s\u1eb5n trong h\u1ea7u h\u1ebft c\u00e1c h\u1ec7 \u0111i\u1ec1u h\u00e0nh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u1ea3 n\u0103ng t\u00edch h\u1ee3p<\/b><span style=\"font-weight: 400;\">: Bash script c\u00f3 th\u1ec3 t\u00edch h\u1ee3p v\u1edbi c\u00e1c c\u00f4ng c\u1ee5 v\u00e0 \u1ee9ng d\u1ee5ng kh\u00e1c nh\u01b0 c\u01a1 s\u1edf d\u1eef li\u1ec7u, m\u00e1y ch\u1ee7 web hay d\u1ecbch v\u1ee5 \u0111\u00e1m m\u00e2y, cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng v\u00e0 t\u1ef1 \u0111\u1ed9ng h\u00f3a ph\u1ee9c t\u1ea1p h\u01a1n.<\/span><\/li>\n<\/ul>\n<h3><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a Bash Script l\u00e0 g\u00ec?<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ch\u1eadm h\u01a1n ch\u01b0\u01a1ng tr\u00ecnh th\u00f4ng th\u01b0\u1eddng<\/b><span style=\"font-weight: 400;\">: So v\u1edbi c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh \u0111\u01b0\u1ee3c bi\u00ean d\u1ecbch nh\u01b0 C hay Java, Bash script th\u01b0\u1eddng ch\u1ea1y ch\u1eadm h\u01a1n do n\u00f3 ph\u1ea3i th\u1ef1c thi t\u1eebng l\u1ec7nh trong script theo d\u1ea1ng d\u00f2ng l\u1ec7nh, thay v\u00ec th\u1ef1c thi nh\u01b0 m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh \u0111\u00e3 bi\u00ean d\u1ecbch s\u1eb5n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u1ea3 n\u0103ng s\u1eed d\u1ee5ng h\u1ea1n ch\u1ebf<\/b><span style=\"font-weight: 400;\">: M\u1eb7c d\u00f9 Bash script r\u1ea5t h\u1eefu \u00edch trong vi\u1ec7c t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 h\u1ec7 th\u1ed1ng c\u01a1 b\u1ea3n, nh\u01b0ng n\u00f3 c\u00f3 h\u1ea1n ch\u1ebf khi so v\u1edbi c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh truy\u1ec1n th\u1ed1ng. Bash kh\u00f4ng ph\u1ea3i l\u00e0 ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh \u0111\u1ea7y \u0111\u1ee7, v\u00ec v\u1eady n\u00f3 kh\u00f4ng ph\u00f9 h\u1ee3p cho nh\u1eefng t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p ho\u1eb7c y\u00eau c\u1ea7u giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng \u0111\u1ed3 h\u1ecda (GUI).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u1edfi \u0111\u1ed9ng qu\u00e1 tr\u00ecnh m\u1edbi cho m\u1ed7i l\u1ec7nh<\/b><span style=\"font-weight: 400;\">: M\u1ed7i khi th\u1ef1c thi m\u1ed9t l\u1ec7nh trong Bash script, m\u1ed9t ti\u1ebfn tr\u00ecnh m\u1edbi \u0111\u01b0\u1ee3c kh\u1edfi \u0111\u1ed9ng. \u0110i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 l\u00e0m t\u1ed1n t\u00e0i nguy\u00ean h\u1ec7 th\u1ed1ng, \u0111\u1eb7c bi\u1ec7t l\u00e0 khi script tr\u1edf n\u00ean ph\u1ee9c t\u1ea1p v\u1edbi nhi\u1ec1u l\u1ec7nh li\u00ean ti\u1ebfp, khi\u1ebfn hi\u1ec7u su\u1ea5t b\u1ecb gi\u1ea3m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>V\u1ec1 t\u00ednh b\u1ea3o m\u1eadt<\/b><span style=\"font-weight: 400;\">: Bash script c\u00f3 th\u1ec3 g\u1eb7p l\u1ed7 h\u1ed5ng b\u1ea3o m\u1eadt n\u1ebfu kh\u00f4ng x\u1eed l\u00fd \u0111\u00fang \u0111\u1ea7u v\u00e0o ng\u01b0\u1eddi d\u00f9ng, \u0111\u1eb7c bi\u1ec7t l\u00e0 khi ch\u1ea1y v\u1edbi quy\u1ec1n root ho\u1eb7c sudo.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ednh di \u0111\u1ed9ng gi\u1eefa c\u00e1c phi\u00ean b\u1ea3n shell<\/b><span style=\"font-weight: 400;\">: Script c\u00f3 th\u1ec3 ho\u1ea1t \u0111\u1ed9ng kh\u00e1c nhau tr\u00ean c\u00e1c phi\u00ean b\u1ea3n bash kh\u00e1c nhau ho\u1eb7c tr\u00ean c\u00e1c shell kh\u00e1c nh\u01b0 sh, zsh, etc.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>H\u1ea1n ch\u1ebf v\u1ec1 kh\u1ea3 n\u0103ng g\u1ee1 l\u1ed7i<\/b><span style=\"font-weight: 400;\">:<\/span><span style=\"font-weight: 400;\"> Bash h\u1ea1n ch\u1ebf trong vi\u1ec7c g\u1ee1 l\u1ed7i so v\u1edbi c\u00e1c ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh hi\u1ec7n \u0111\u1ea1i kh\u00e1c. M\u1eb7c d\u00f9 c\u00f3 c\u00e1c t\u00f9y ch\u1ecdn nh\u01b0 -x \u0111\u1ec3 hi\u1ec3n th\u1ecb t\u1eebng l\u1ec7nh khi th\u1ef1c thi, nh\u01b0ng Bash kh\u00f4ng c\u00f3 c\u00f4ng c\u1ee5 g\u1ee1 l\u1ed7i t\u00edch h\u1ee3p \u0111\u1ea7y \u0111\u1ee7 nh\u01b0 c\u00e1c IDE hi\u1ec7n \u0111\u1ea1i.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Bash_script_hoat_dong_nhu_the_nao\"><\/span><b>Bash script ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Tr\u01b0\u1edbc khi t\u00ecm hi\u1ec3u c\u00e1ch Bash script ho\u1ea1t \u0111\u1ed9ng, ch\u00fang ta c\u1ea7n n\u1eafm m\u1ed9t s\u1ed1 kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n v\u1ec1 ch\u01b0\u01a1ng tr\u00ecnh (program) v\u00e0 qu\u00e1 tr\u00ecnh (process) trong m\u00e1y t\u00ednh.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ch\u01b0\u01a1ng tr\u00ecnh<\/b><span style=\"font-weight: 400;\">: l\u00e0 m\u1ed9t t\u1ec7p d\u1eef li\u1ec7u ch\u1ee9a c\u00e1c h\u01b0\u1edbng d\u1eabn m\u00e0 CPU c\u1ea7n th\u1ef1c hi\u1ec7n, v\u00ed d\u1ee5 nh\u01b0 c\u00e1c t\u1ec7p nh\u1ecb ph\u00e2n ho\u1eb7c m\u00e3 ngu\u1ed3n \u0111\u00e3 \u0111\u01b0\u1ee3c bi\u00ean d\u1ecbch. Ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 th\u1ec3 bao g\u1ed3m c\u00e1c t\u00e0i nguy\u00ean kh\u00e1c nh\u01b0 h\u00ecnh \u1ea3nh, \u00e2m thanh, v.v., v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c l\u01b0u tr\u00ean \u1ed5 c\u1ee9ng c\u1ee7a m\u00e1y t\u00ednh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Qu\u00e1 tr\u00ecnh (process)<\/b><span style=\"font-weight: 400;\">: l\u00e0 m\u1ed9t phi\u00ean b\u1ea3n \u0111ang ch\u1ea1y c\u1ee7a m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh. Khi b\u1ea1n ch\u1ea1y m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh, h\u1ec7 \u0111i\u1ec1u h\u00e0nh kh\u00f4ng tr\u1ef1c ti\u1ebfp th\u1ef1c thi ch\u01b0\u01a1ng tr\u00ecnh \u0111\u00f3 t\u1eeb \u1ed5 c\u1ee9ng m\u00e0 n\u00f3 sao ch\u00e9p c\u00e1c h\u01b0\u1edbng d\u1eabn c\u1ee7a ch\u01b0\u01a1ng tr\u00ecnh v\u00e0o b\u1ed9 nh\u1edb RAM v\u00e0 kh\u1edfi t\u1ea1o m\u1ed9t qu\u00e1 tr\u00ecnh m\u1edbi \u0111\u1ec3 th\u1ef1c thi c\u00e1c l\u1ec7nh. M\u1ed7i qu\u00e1 tr\u00ecnh s\u1ebd c\u00f3 kh\u00f4ng gian b\u1ed9 nh\u1edb ri\u00eang \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c bi\u1ebfn t\u1ea1m th\u1eddi v\u00e0 c\u00e1c d\u1eef li\u1ec7u c\u1ea7n thi\u1ebft trong qu\u00e1 tr\u00ecnh th\u1ef1c thi.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Khi ta m\u1edf m\u1ed9t terminal v\u00e0 th\u1ef1c hi\u1ec7n m\u1ed9t l\u1ec7nh nh\u01b0 <\/span><span style=\"font-weight: 400;\">cp<\/span><span style=\"font-weight: 400;\"> (sao ch\u00e9p t\u1ec7p), h\u1ec7 th\u1ed1ng s\u1ebd t\u1ea1o ra m\u1ed9t qu\u00e1 tr\u00ecnh m\u1edbi cho l\u1ec7nh \u0111\u00f3. N\u1ebfu m\u1edf nhi\u1ec1u terminal v\u00e0 ch\u1ea1y c\u00f9ng m\u1ed9t l\u1ec7nh, m\u1ed7i terminal s\u1ebd t\u1ea1o ra m\u1ed9t qu\u00e1 tr\u00ecnh ri\u00eang bi\u1ec7t cho l\u1ec7nh <\/span><span style=\"font-weight: 400;\">cp<\/span><span style=\"font-weight: 400;\">. Sau khi qu\u00e1 tr\u00ecnh ho\u00e0n th\u00e0nh, h\u1ec7 th\u1ed1ng s\u1ebd h\u1ee7y b\u1ecf qu\u00e1 tr\u00ecnh \u0111\u00f3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Khi ch\u1ea1y m\u1ed9t Bash script, n\u00f3 s\u1ebd kh\u00f4ng ch\u1ea1y trong qu\u00e1 tr\u00ecnh Bash hi\u1ec7n t\u1ea1i m\u00e0 thay v\u00e0o \u0111\u00f3 s\u1ebd t\u1ea1o ra m\u1ed9t qu\u00e1 tr\u00ecnh m\u1edbi \u0111\u1ec3 th\u1ef1c thi c\u00e1c l\u1ec7nh trong script. Qu\u00e1 tr\u00ecnh n\u00e0y l\u00e0m vi\u1ec7c \u0111\u1ed9c l\u1eadp v\u00e0 s\u1ebd b\u1ecb h\u1ec7 th\u1ed1ng h\u1ee7y b\u1ecf khi script ho\u00e0n th\u00e0nh.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Mot_so_khai_niem_co_ban_cua_Bash_script\"><\/span><b>M\u1ed9t s\u1ed1 kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n c\u1ee7a Bash script<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>Quy \u01b0\u1edbc \u0111\u1eb7t t\u00ean<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Bash scripting, vi\u1ec7c \u0111\u1eb7t t\u00ean t\u1ec7p v\u00e0 bi\u1ebfn r\u1ea5t quan tr\u1ecdng \u0111\u1ec3 d\u1ec5 qu\u1ea3n l\u00fd v\u00e0 duy tr\u00ec m\u00e3 ngu\u1ed3n. M\u1ed9t s\u1ed1 quy \u01b0\u1edbc \u0111\u1eb7t t\u00ean bi\u1ebfn trong Bash:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn ph\u1ea3i b\u1eaft \u0111\u1ea7u b\u1eb1ng m\u1ed9t ch\u1eef c\u00e1i ho\u1eb7c d\u1ea5u g\u1ea1ch d\u01b0\u1edbi (<\/span><span style=\"font-weight: 400;\">_<\/span><span style=\"font-weight: 400;\">)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn c\u00f3 th\u1ec3 ch\u1ee9a ch\u1eef c\u00e1i, s\u1ed1 v\u00e0 d\u1ea5u g\u1ea1ch d\u01b0\u1edbi.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn kh\u00f4ng \u0111\u01b0\u1ee3c ch\u1ee9a kho\u1ea3ng tr\u1eafng ho\u1eb7c k\u00fd t\u1ef1 \u0111\u1eb7c bi\u1ec7t.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn trong Bash ph\u00e2n bi\u1ec7t ch\u1eef hoa v\u00e0 ch\u1eef th\u01b0\u1eddng.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn kh\u00f4ng \u0111\u01b0\u1ee3c tr\u00f9ng v\u1edbi c\u00e1c t\u1eeb kh\u00f3a \u0111\u00e3 \u0111\u01b0\u1ee3c h\u1ec7 th\u1ed1ng d\u00e0nh ri\u00eang, nh\u01b0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">then<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">fi<\/span><span style=\"font-weight: 400;\">\u2026\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c h\u1eb1ng s\u1ed1 th\u01b0\u1eddng \u0111\u01b0\u1ee3c vi\u1ebft hoa ho\u00e0n to\u00e0n.<\/span><\/li>\n<\/ul>\n<h3><b>\u00dd ngh\u0129a c\u1ee7a <\/b><span style=\"font-weight: 400;\">.\/<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Khi b\u1ea1n ch\u1ea1y m\u1ed9t l\u1ec7nh th\u00f4ng th\u01b0\u1eddng tr\u00ean terminal nh\u01b0 <\/span><b>ls<\/b><span style=\"font-weight: 400;\">, b\u1ea1n ch\u1ec9 c\u1ea7n g\u00f5 t\u00ean l\u1ec7nh v\u00e0 h\u1ec7 th\u1ed1ng s\u1ebd th\u1ef1c thi n\u00f3.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, khi ch\u1ea1y m\u1ed9t Bash script trong th\u01b0 m\u1ee5c hi\u1ec7n t\u1ea1i, b\u1ea1n c\u1ea7n ph\u1ea3i s\u1eed d\u1ee5ng d\u1ea5u <\/span><b>.\/<\/b><span style=\"font-weight: 400;\"> tr\u01b0\u1edbc t\u00ean script. Vi\u1ec7c n\u00e0y l\u00e0 do Bash kh\u00f4ng t\u1ef1 \u0111\u1ed9ng t\u00ecm ki\u1ebfm trong th\u01b0 m\u1ee5c hi\u1ec7n t\u1ea1i khi b\u1ea1n ch\u1ec9 g\u00f5 t\u00ean l\u1ec7nh. Thay v\u00e0o \u0111\u00f3, n\u00f3 ch\u1ec9 t\u00ecm l\u1ec7nh trong c\u00e1c th\u01b0 m\u1ee5c \u0111\u01b0\u1ee3c li\u1ec7t k\u00ea trong bi\u1ebfn m\u00f4i tr\u01b0\u1eddng <\/span><span style=\"font-weight: 400;\">$PATH<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$PATH<\/span><span style=\"font-weight: 400;\"> l\u00e0 bi\u1ebfn ch\u1ee9a m\u1ed9t danh s\u00e1ch c\u00e1c th\u01b0 m\u1ee5c, n\u01a1i m\u00e0 Bash t\u00ecm ki\u1ebfm c\u00e1c l\u1ec7nh. N\u1ebfu b\u1ea1n kh\u00f4ng ch\u1ec9 \u0111\u1ecbnh r\u00f5 n\u01a1i ch\u1ee9a script, Bash s\u1ebd kh\u00f4ng th\u1ec3 t\u00ecm th\u1ea5y n\u00f3 trong th\u01b0 m\u1ee5c hi\u1ec7n t\u1ea1i<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5, khi g\u00f5 l\u1ec7nh <\/span><span style=\"font-weight: 400;\">echo $PATH<\/span><span style=\"font-weight: 400;\">, b\u1ea1n s\u1ebd th\u1ea5y danh s\u00e1ch c\u00e1c th\u01b0 m\u1ee5c nh\u01b0:<\/span><\/p>\n<pre>\/home\/ryan\/bin:\/usr\/local\/bin:\/usr\/bin:\/bin<\/pre>\n<p><span style=\"font-weight: 400;\">D\u00f2ng tr\u00ean c\u00f3 ngh\u0129a l\u00e0 Bash s\u1ebd ch\u1ec9 t\u00ecm ki\u1ebfm l\u1ec7nh trong c\u00e1c th\u01b0 m\u1ee5c n\u00e0y. N\u1ebfu b\u1ea1n kh\u00f4ng th\u00eam <\/span><span style=\"font-weight: 400;\">.\/<\/span><span style=\"font-weight: 400;\"> v\u00e0 ch\u1ec9 g\u00f5 t\u00ean script, Bash s\u1ebd kh\u00f4ng t\u00ecm th\u1ea5y n\u00f3 trong <\/span><span style=\"font-weight: 400;\">$PATH<\/span><span style=\"font-weight: 400;\"> v\u00ec th\u01b0 m\u1ee5c hi\u1ec7n t\u1ea1i kh\u00f4ng n\u1eb1m trong danh s\u00e1ch n\u00e0y.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ec v\u1eady, khi b\u1ea1n s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">.\/<\/span><span style=\"font-weight: 400;\">, b\u1ea1n \u0111ang ch\u1ec9 r\u00f5 Bash t\u00ecm ki\u1ebfm script trong th\u01b0 m\u1ee5c hi\u1ec7n t\u1ea1i. \u0110\u00e2y l\u00e0 c\u00e1ch an to\u00e0n v\u00e0 r\u00f5 r\u00e0ng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o r\u1eb1ng b\u1ea1n \u0111ang ch\u1ea1y \u0111\u00fang t\u1ec7p script mong mu\u1ed1n.<\/span><\/p>\n<h3><b>Shebang [#]<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong m\u1ed9t Bash script, shebang l\u00e0 d\u00f2ng \u0111\u1ea7u ti\u00ean c\u1ee7a t\u1ec7p v\u00e0 c\u00f3 vai tr\u00f2 r\u1ea5t quan tr\u1ecdng. Shebang b\u1eaft \u0111\u1ea7u b\u1eb1ng k\u00fd t\u1ef1 <\/span><span style=\"font-weight: 400;\">#!<\/span><span style=\"font-weight: 400;\">, theo sau l\u00e0 \u0111\u01b0\u1eddng d\u1eabn \u0111\u1ebfn tr\u00ecnh th\u00f4ng d\u1ecbch (interpreter) m\u00e0 script c\u1ea7n s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c thi c\u00e1c l\u1ec7nh trong t\u1ec7p.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed1i v\u1edbi Bash script, shebang th\u01b0\u1eddng c\u00f3 d\u1ea1ng <\/span><span style=\"font-weight: 400;\">#!\/bin\/bash<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">D\u00f2ng shebang cho h\u1ec7 \u0111i\u1ec1u h\u00e0nh bi\u1ebft r\u1eb1ng t\u1ec7p n\u00e0y l\u00e0 m\u1ed9t script v\u00e0 c\u1ea7n \u0111\u01b0\u1ee3c th\u1ef1c thi b\u1edfi Bash shell. \u0110\u00e2y l\u00e0 c\u00e1ch \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh tr\u00ecnh th\u00f4ng d\u1ecbch cho script m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i ch\u1ec9 \u0111\u1ecbnh tr\u1ef1c ti\u1ebfp khi ch\u1ea1y t\u1ec7p. N\u1ebfu kh\u00f4ng c\u00f3 shebang, Bash s\u1ebd m\u1eb7c \u0111\u1ecbnh xem script \u0111\u00f3 l\u00e0 m\u1ed9t Bash script n\u1ebfu b\u1ea1n ch\u1ea1y n\u00f3 trong Bash shell.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 v\u00ed d\u1ee5 v\u1ec1 shebang v\u1edbi c\u00e1c tr\u00ecnh th\u00f4ng d\u1ecbch kh\u00e1c nhau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">#!\/bin\/bash<\/span><span style=\"font-weight: 400;\"> \u2014 S\u1eed d\u1ee5ng Bash.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">#!\/bin\/sh<\/span><span style=\"font-weight: 400;\"> \u2014 S\u1eed d\u1ee5ng Bourne shell.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">#!\/usr\/bin\/env python<\/span><span style=\"font-weight: 400;\"> \u2014 S\u1eed d\u1ee5ng Python (v\u1edbi c\u00f4ng c\u1ee5 env \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng t\u00ecm \u0111\u01b0\u1eddng d\u1eabn \u0111\u1ebfn Python).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">#!\/usr\/bin\/pwsh<\/span><span style=\"font-weight: 400;\"> \u2014 S\u1eed d\u1ee5ng PowerShell.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">M\u1eb7c d\u00f9 c\u00f3 th\u1ec3 b\u1ecf qua shebang v\u00e0 ch\u1ea1y script b\u1eb1ng c\u00e1ch ch\u1ec9 \u0111\u1ecbnh <\/span><span style=\"font-weight: 400;\">bash myscript.sh<\/span><span style=\"font-weight: 400;\">, nh\u01b0ng \u0111i\u1ec1u n\u00e0y kh\u00f4ng ph\u1ea3i l\u00e0 c\u00e1ch an to\u00e0n v\u00e0 ti\u1ec7n l\u1ee3i nh\u1ea5t. V\u00ec v\u1eady, vi\u1ec7c lu\u00f4n lu\u00f4n th\u00eam shebang v\u00e0o \u0111\u1ea7u m\u1ed7i Bash script l\u00e0 c\u00e1ch t\u1ed1t nh\u1ea5t \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh t\u01b0\u01a1ng th\u00edch v\u00e0 d\u1ec5 d\u00e0ng th\u1ef1c thi.<\/span><\/p>\n<h3><b>H\u00e0m (function)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Bash script, h\u00e0m l\u00e0 m\u1ed9t \u0111o\u1ea1n m\u00e3 \u0111\u01b0\u1ee3c nh\u00f3m l\u1ea1i v\u1edbi nhau \u0111\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t nhi\u1ec7m v\u1ee5 c\u1ee5 th\u1ec3. H\u00e0m gi\u00fap m\u00e3 ngu\u1ed3n tr\u1edf n\u00ean g\u1ecdn g\u00e0ng h\u01a1n, d\u1ec5 d\u00e0ng t\u00e1i s\u1eed d\u1ee5ng v\u00e0 b\u1ea3o tr\u00ec.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Khi b\u1ea1n c\u1ea7n th\u1ef1c hi\u1ec7n m\u1ed9t t\u00e1c v\u1ee5 nhi\u1ec1u l\u1ea7n trong script, thay v\u00ec vi\u1ebft l\u1ea1i c\u00f9ng m\u1ed9t \u0111o\u1ea1n m\u00e3, b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1ecbnh ngh\u0129a h\u00e0m v\u00e0 g\u1ecdi h\u00e0m \u0111\u00f3 b\u1ea5t k\u1ef3 l\u00fac n\u00e0o.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u1ea5u tr\u00fac c\u01a1 b\u1ea3n \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a m\u1ed9t h\u00e0m trong Bash nh\u01b0 sau:<\/span><\/p>\n<pre>my_function() {\n\n\u00a0\u00a0echo \"Hello, World!\"\n\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3: <\/span><span style=\"font-weight: 400;\">my_function<\/span><span style=\"font-weight: 400;\"> l\u00e0 t\u00ean c\u1ee7a h\u00e0m v\u00e0 l\u1ec7nh b\u00ean trong d\u1ea5u {} l\u00e0 m\u00e3 m\u00e0 h\u00e0m th\u1ef1c thi. \u0110\u1ec3 g\u1ecdi h\u00e0m n\u00e0y, b\u1ea1n ch\u1ec9 c\u1ea7n vi\u1ebft t\u00ean h\u00e0m trong script nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">my_function<\/span><\/pre>\n<h3><b>X\u1eed l\u00fd l\u1ed7i v\u00e0 debug<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi vi\u1ebft Bash script, vi\u1ec7c x\u1eed l\u00fd l\u1ed7i v\u00e0 debug r\u1ea5t quan tr\u1ecdng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o script ch\u1ea1y \u0111\u00fang v\u00e0 d\u1ec5 d\u00e0ng kh\u1eafc ph\u1ee5c khi g\u1eb7p v\u1ea5n \u0111\u1ec1. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 k\u1ef9 thu\u1eadt c\u01a1 b\u1ea3n gi\u00fap b\u1ea1n x\u1eed l\u00fd l\u1ed7i v\u00e0 debug trong Bash script:<\/span><\/p>\n<h4><strong>Chuy\u1ec3n h\u01b0\u1edbng th\u00f4ng b\u00e1o l\u1ed7i<\/strong><\/h4>\n<p>\u0110\u1ec3 ghi l\u1ea1i l\u1ed7i v\u00e0o m\u1ed9t t\u1ec7p, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng 2&gt;, v\u00ed d\u1ee5:<\/p>\n<pre>command 2&gt; error_log.txt<\/pre>\n<p>L\u1ec7nh tr\u00ean s\u1ebd chuy\u1ec3n c\u00e1c th\u00f4ng b\u00e1o l\u1ed7i (STDERR) c\u1ee7a command v\u00e0o t\u1ec7p error_log.txt.<\/p>\n<h4><strong>D\u1eebng script khi g\u1eb7p l\u1ed7i<\/strong><\/h4>\n<p>B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng set -e \u0111\u1ec3 d\u1eebng th\u1ef1c thi script ngay khi g\u1eb7p l\u1ed7i. \u0110i\u1ec1u n\u00e0y gi\u00fap tr\u00e1nh c\u00e1c v\u1ea5n \u0111\u1ec1 ph\u00e1t sinh khi script ti\u1ebfp t\u1ee5c ch\u1ea1y d\u00f9 g\u1eb7p l\u1ed7i.<\/p>\n<pre>set -e<\/pre>\n<h4><strong>Debug v\u1edbi set -x<\/strong><\/h4>\n<p>\u0110\u1ec3 theo d\u00f5i c\u00e1c l\u1ec7nh \u0111\u01b0\u1ee3c th\u1ef1c thi trong script, b\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng set -x. L\u1ec7nh n\u00e0y s\u1ebd in ra t\u1eebng l\u1ec7nh khi n\u00f3 \u0111\u01b0\u1ee3c th\u1ef1c thi, gi\u00fap b\u1ea1n d\u1ec5 d\u00e0ng ph\u00e1t hi\u1ec7n l\u1ed7i.<\/p>\n<h4><strong>K\u1ebft h\u1ee3p STDOUT v\u00e0 STDERR<\/strong><\/h4>\n<p>\u0110\u1ec3 ghi c\u1ea3 k\u1ebft qu\u1ea3 \u0111\u1ea7u ra (STDOUT) v\u00e0 l\u1ed7i (STDERR) v\u00e0o c\u00f9ng m\u1ed9t t\u1ec7p, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng 2&gt;&amp;1. \u0110i\u1ec1u n\u00e0y gi\u00fap b\u1ea1n c\u00f3 m\u1ed9t b\u1ea3n ghi \u0111\u1ea7y \u0111\u1ee7 c\u1ee7a t\u1ea5t c\u1ea3 nh\u1eefng g\u00ec x\u1ea3y ra khi script ch\u1ea1y.<\/p>\n<pre>command &gt; output_log.txt 2&gt;&amp;1<\/pre>\n<h4><strong>S\u1eed d\u1ee5ng trap \u0111\u1ec3 b\u1eaft t\u00edn hi\u1ec7u<\/strong><\/h4>\n<p>trap l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd cho ph\u00e9p b\u1ea1n b\u1eaft v\u00e0 x\u1eed l\u00fd c\u00e1c t\u00edn hi\u1ec7u h\u1ec7 th\u1ed1ng (nh\u01b0 Ctrl+C) ho\u1eb7c c\u00e1c s\u1ef1 ki\u1ec7n kh\u00e1c trong script. \u0110i\u1ec1u n\u00e0y \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch khi b\u1ea1n c\u1ea7n d\u1ecdn d\u1eb9p t\u00e0i nguy\u00ean tr\u01b0\u1edbc khi script k\u1ebft th\u00fac.<\/p>\n<pre><span style=\"font-weight: 400;\">#!\/bin\/bash<\/span>\n\n<span style=\"font-weight: 400;\"># T\u1ea1o file t\u1ea1m th\u1eddi<\/span>\n<span style=\"font-weight: 400;\">TEMP_FILE=\"\/tmp\/mydata_$$.tmp\"<\/span>\n<span style=\"font-weight: 400;\">touch $TEMP_FILE\n<\/span>\n<span style=\"font-weight: 400;\"># H\u00e0m d\u1ecdn d\u1eb9p s\u1ebd \u0111\u01b0\u1ee3c g\u1ecdi khi script k\u1ebft th\u00fac<\/span>\n<span style=\"font-weight: 400;\">cleanup() {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0echo \"\u0110ang d\u1ecdn d\u1eb9p t\u00e0i nguy\u00ean...\"<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0rm -f $TEMP_FILE<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0echo \"\u0110\u00e3 x\u00f3a file t\u1ea1m: $TEMP_FILE\"<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\"># \u0110\u0103ng k\u00fd h\u00e0m cleanup \u0111\u1ec3 x\u1eed l\u00fd khi nh\u1eadn c\u00e1c t\u00edn hi\u1ec7u<\/span>\n<span style=\"font-weight: 400;\"># EXIT: Khi script k\u1ebft th\u00fac b\u00ecnh th\u01b0\u1eddng<\/span>\n<span style=\"font-weight: 400;\"># SIGINT: Khi ng\u01b0\u1eddi d\u00f9ng nh\u1ea5n Ctrl+C (gi\u00e1n \u0111o\u1ea1n)<\/span>\n<span style=\"font-weight: 400;\"># SIGTERM: Khi script b\u1ecb y\u00eau c\u1ea7u k\u1ebft th\u00fac (terminate)<\/span>\n<span style=\"font-weight: 400;\">trap cleanup EXIT SIGINT SIGTERM<\/span>\n\n<span style=\"font-weight: 400;\">echo \"\u0110ang th\u1ef1c hi\u1ec7n c\u00f4ng vi\u1ec7c...\"<\/span>\n<span style=\"font-weight: 400;\">echo \"D\u1eef li\u1ec7u t\u1ea1m th\u1eddi \u0111\u01b0\u1ee3c l\u01b0u t\u1ea1i: $TEMP_FILE\"<\/span>\n\n<span style=\"font-weight: 400;\"># Gi\u1ea3 l\u1eadp c\u00f4ng vi\u1ec7c \u0111ang ch\u1ea1y<\/span>\n<span style=\"font-weight: 400;\">echo \"Script \u0111ang ch\u1ea1y, th\u1eed nh\u1ea5n Ctrl+C \u0111\u1ec3 xem trap ho\u1ea1t \u0111\u1ed9ng...\"<\/span>\n<span style=\"font-weight: 400;\">sleep 10<\/span>\n\n<span style=\"font-weight: 400;\">echo \"Script ho\u00e0n th\u00e0nh c\u00f4ng vi\u1ec7c!\"<\/span>\n<span style=\"font-weight: 400;\"># Khi script k\u1ebft th\u00fac, h\u00e0m cleanup s\u1ebd t\u1ef1 \u0111\u1ed9ng \u0111\u01b0\u1ee3c g\u1ecdi<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 n\u00e0y:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Script t\u1ea1o m\u1ed9t file t\u1ea1m th\u1eddi v\u00e0 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">trap<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o file n\u00e0y s\u1ebd \u0111\u01b0\u1ee3c x\u00f3a khi script k\u1ebft th\u00fac.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u00fang ta b\u1eaft 3 lo\u1ea1i t\u00edn hi\u1ec7u: <\/span><span style=\"font-weight: 400;\">EXIT<\/span><span style=\"font-weight: 400;\"> (khi script k\u1ebft th\u00fac b\u00ecnh th\u01b0\u1eddng), <\/span><span style=\"font-weight: 400;\">SIGINT<\/span><span style=\"font-weight: 400;\"> (khi ng\u01b0\u1eddi d\u00f9ng nh\u1ea5n Ctrl+C), v\u00e0 <\/span><span style=\"font-weight: 400;\">SIGTERM<\/span><span style=\"font-weight: 400;\"> (khi c\u00f3 l\u1ec7nh terminate \u0111\u01b0\u1ee3c g\u1eedi \u0111\u1ebfn script).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1ea5t k\u1ec3 script k\u1ebft th\u00fac theo c\u00e1ch n\u00e0o (b\u00ecnh th\u01b0\u1eddng ho\u1eb7c b\u1ecb gi\u00e1n \u0111o\u1ea1n), h\u00e0m <\/span><span style=\"font-weight: 400;\">cleanup()<\/span><span style=\"font-weight: 400;\"> s\u1ebd lu\u00f4n \u0111\u01b0\u1ee3c g\u1ecdi \u0111\u1ec3 d\u1ecdn d\u1eb9p t\u00e0i nguy\u00ean.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Chay_Bash_script_nhu_the_nao\"><\/span><b>Ch\u1ea1y Bash script nh\u01b0 th\u1ebf n\u00e0o?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c ch\u1ea1y m\u1ed9t Bash script kh\u00e1 \u0111\u01a1n gi\u1ea3n, nh\u01b0ng tr\u01b0\u1edbc khi th\u1ef1c thi, b\u1ea1n c\u1ea7n \u0111\u1ea3m b\u1ea3o r\u1eb1ng t\u1ec7p script \u0111\u00e3 \u0111\u01b0\u1ee3c c\u1ea5p quy\u1ec1n th\u1ef1c thi (execute permission). Th\u00f4ng th\u01b0\u1eddng, quy\u1ec1n n\u00e0y kh\u00f4ng \u0111\u01b0\u1ee3c c\u1ea5p m\u1eb7c \u0111\u1ecbnh v\u00ec l\u00fd do b\u1ea3o m\u1eadt. N\u1ebfu b\u1ea1n qu\u00ean c\u1ea5p quy\u1ec1n th\u1ef1c thi, h\u1ec7 th\u1ed1ng s\u1ebd th\u00f4ng b\u00e1o l\u1ed7i m\u00e0 kh\u00f4ng g\u00e2y h\u1ea1i g\u00ec.<\/span><\/p>\n<h3><b>C\u1ea5p quy\u1ec1n th\u1ef1c thi cho script<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Tr\u01b0\u1edbc ti\u00ean, b\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m tra quy\u1ec1n c\u1ee7a t\u1ec7p script b\u1eb1ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">ls -l<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre>ls -l myscript.sh\n\n-rw-r--r-- 18 ryan users 4096 Feb 17 09:12 myscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">Theo nh\u01b0 d\u00f2ng l\u1ec7nh tr\u00ean th\u00ec t\u1ec7p <\/span><span style=\"font-weight: 400;\">myscript.sh<\/span><span style=\"font-weight: 400;\"> hi\u1ec7n t\u1ea1i kh\u00f4ng c\u00f3 quy\u1ec1n th\u1ef1c thi. \u0110\u1ec3 c\u1ea5p quy\u1ec1n n\u00e0y, b\u1ea1n s\u1eed d\u1ee5ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">chmod<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre>chmod 755 myscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh n\u00e0y s\u1ebd c\u1ea5p quy\u1ec1n th\u1ef1c thi cho ch\u1ee7 s\u1edf h\u1eefu, nh\u00f3m v\u00e0 t\u1ea5t c\u1ea3 ng\u01b0\u1eddi d\u00f9ng. Sau khi c\u1ea5p quy\u1ec1n, b\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m tra l\u1ea1i quy\u1ec1n c\u1ee7a t\u1ec7p:<\/span><\/p>\n<pre>ls -l myscript.sh\n\n-rwxr-xr-x 18 ryan users 4096 Feb 17 09:12 myscript.sh<\/pre>\n<h3><b>Ch\u1ea1y script<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi t\u1ec7p \u0111\u00e3 c\u00f3 quy\u1ec1n th\u1ef1c thi, b\u1ea1n c\u00f3 th\u1ec3 ch\u1ea1y script b\u1eb1ng l\u1ec7nh sau:<\/span><\/p>\n<pre>.\/myscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">Ho\u1eb7c:<\/span><\/p>\n<pre>bash myscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh n\u00e0y s\u1ebd th\u1ef1c thi script v\u00e0 in ra k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<pre>Hello World!<\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi tr\u01b0\u1eddng h\u1ee3p truy\u1ec1n tham s\u1ed1 th\u00ec c\u00f3 th\u1ec3 ch\u1ea1y script nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">.\/myscript.sh param1 param2<\/span><\/pre>\n<h3><b>Ch\u1ea1y d\u01b0\u1edbi background<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 ch\u1ea1y m\u1ed9t s\u1ed1 c\u00f4ng vi\u1ec7c d\u01b0\u1edbi n\u1ec1n:<\/span><\/p>\n<pre>.\/myscript.sh &amp;<\/pre>\n<p><span style=\"font-weight: 400;\">V\u00e0 c\u00e1ch s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">nohup <\/span><span style=\"font-weight: 400;\">\u0111\u1ec3 script ti\u1ebfp t\u1ee5c ch\u1ea1y ngay c\u1ea3 khi ng\u01b0\u1eddi d\u00f9ng \u0111\u0103ng xu\u1ea5t:<\/span><\/p>\n<pre>nohup .\/myscript.sh &amp;<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Cac_phuong_phap_de_su_dung_Bash_script_hieu_qua_hon\"><\/span><b>C\u00e1c ph\u01b0\u01a1ng ph\u00e1p \u0111\u1ec3 s\u1eed d\u1ee5ng Bash script hi\u1ec7u qu\u1ea3 h\u01a1n<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Th\u00eam c\u00e1c l\u1ec7nh ki\u1ec3m tra l\u1ed7i<\/b><span style=\"font-weight: 400;\">: \u0110\u1ec3 tr\u00e1nh l\u1ed7i khi ch\u1ea1y script, s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">set -e<\/span><span style=\"font-weight: 400;\"> \u1edf \u0111\u1ea7u script \u0111\u1ec3 d\u1eebng script n\u1ebfu c\u00f3 l\u1ed7i x\u1ea3y ra, gi\u00fap b\u1ea1n tr\u00e1nh \u0111\u01b0\u1ee3c nh\u1eefng v\u1ea5n \u0111\u1ec1 kh\u00f4ng mong mu\u1ed1n trong qu\u00e1 tr\u00ecnh th\u1ef1c thi.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng h\u00e0m cho c\u00e1c t\u00e1c v\u1ee5 l\u1eb7p l\u1ea1i<\/b><span style=\"font-weight: 400;\">: Khi b\u1ea1n c\u1ea7n th\u1ef1c hi\u1ec7n c\u00f9ng m\u1ed9t t\u00e1c v\u1ee5 nhi\u1ec1u l\u1ea7n trong script, h\u00e3y \u0111\u00f3ng g\u00f3i ch\u00fang v\u00e0o c\u00e1c h\u00e0m. \u0110i\u1ec1u n\u00e0y gi\u00fap m\u00e3 ngu\u1ed3n g\u1ecdn g\u00e0ng v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Gi\u1eef script ng\u1eafn g\u1ecdn v\u00e0 c\u00f3 ch\u00fa th\u00edch r\u00f5 r\u00e0ng<\/b><span style=\"font-weight: 400;\">: C\u00e1c script n\u00ean \u0111\u01b0\u1ee3c chia nh\u1ecf th\u00e0nh c\u00e1c ph\u1ea7n d\u1ec5 hi\u1ec3u v\u00e0 d\u1ec5 duy tr\u00ec. Th\u00eam ch\u00fa th\u00edch \u0111\u1ea7y \u0111\u1ee7 \u0111\u1ec3 ng\u01b0\u1eddi kh\u00e1c (ho\u1eb7c ch\u00ednh b\u1ea1n trong t\u01b0\u01a1ng lai) c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng hi\u1ec3u m\u00e3 ngu\u1ed3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng h\u1ec7 th\u1ed1ng qu\u1ea3n l\u00fd phi\u00ean b\u1ea3n (nh\u01b0 Git)<\/b><span style=\"font-weight: 400;\">: \u0110\u1ec3 theo d\u00f5i s\u1ef1 thay \u0111\u1ed5i v\u00e0 ph\u00e1t tri\u1ec3n c\u1ee7a script, vi\u1ec7c s\u1eed d\u1ee5ng Git ho\u1eb7c c\u00e1c c\u00f4ng c\u1ee5 t\u01b0\u01a1ng t\u1ef1 gi\u00fap b\u1ea1n qu\u1ea3n l\u00fd phi\u00ean b\u1ea3n v\u00e0 d\u1ec5 d\u00e0ng quay l\u1ea1i phi\u00ean b\u1ea3n tr\u01b0\u1edbc n\u1ebfu c\u1ea7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng cheat sheet Bash<\/b><span style=\"font-weight: 400;\">: C\u00e1c b\u1ea3ng tra c\u1ee9u (cheat sheet) c\u00f3 th\u1ec3 gi\u00fap b\u1ea1n ti\u1ebft ki\u1ec7m th\u1eddi gian h\u1ecdc v\u00e0 t\u00ecm ki\u1ebfm c\u00fa ph\u00e1p khi vi\u1ebft script, gi\u00fap t\u0103ng t\u1ed1c qu\u00e1 tr\u00ecnh h\u1ecdc v\u00e0 \u00e1p d\u1ee5ng ki\u1ebfn th\u1ee9c.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_ve_Bash_script\"><\/span><b>C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 Bash script<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>Powershell c\u00f3 t\u01b0\u01a1ng t\u1ef1 nh\u01b0 Bash script kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Powershell v\u00e0 Bash script \u0111\u1ec1u l\u00e0 c\u00f4ng c\u1ee5 d\u00f2ng l\u1ec7nh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 tr\u00ean h\u1ec7 th\u1ed1ng, nh\u01b0ng ch\u00fang ho\u1ea1t \u0111\u1ed9ng kh\u00e1c nhau. Powershell ch\u1ee7 y\u1ebfu \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng tr\u00ean h\u1ec7 \u0111i\u1ec1u h\u00e0nh Windows v\u00e0 h\u1ed7 tr\u1ee3 c\u00e1c l\u1ec7nh cmdlets, gi\u00fap qu\u1ea3n tr\u1ecb vi\u00ean d\u1ec5 d\u00e0ng qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng Windows.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash script, ng\u01b0\u1ee3c l\u1ea1i, \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf cho c\u00e1c h\u1ec7 \u0111i\u1ec1u h\u00e0nh Unix-like nh\u01b0 Linux v\u00e0 macOS, cung c\u1ea5p c\u00fa ph\u00e1p m\u1ea1nh m\u1ebd h\u01a1n cho vi\u1ec7c t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 trong m\u00f4i tr\u01b0\u1eddng d\u00f2ng l\u1ec7nh. M\u1eb7c d\u00f9 c\u1ea3 hai c\u00f4ng c\u1ee5 \u0111\u1ec1u gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a, Powershell ch\u1ee7 y\u1ebfu d\u00e0nh cho qu\u1ea3n tr\u1ecb h\u1ec7 th\u1ed1ng Windows, trong khi Bash script ho\u1ea1t \u0111\u1ed9ng t\u1ed1t tr\u00ean c\u00e1c h\u1ec7 \u0111i\u1ec1u h\u00e0nh Unix.<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"\/blog\/powershell-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>PowerShell l\u00e0 g\u00ec chi ti\u1ebft: T\u00ednh n\u0103ng, C\u00e0i \u0111\u1eb7t, M\u1eb9o s\u1eed d\u1ee5ng n\u00ean bi\u1ebft<\/strong><\/a><\/em><\/p><\/blockquote>\n<h3><b>Bash Script c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00f4ng vi\u1ec7c kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00f3, Bash Script r\u1ea5t ph\u00f9 h\u1ee3p trong vi\u1ec7c t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 tr\u00ean h\u1ec7 th\u1ed1ng. B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Bash script \u0111\u1ec3 sao l\u01b0u d\u1eef li\u1ec7u, qu\u1ea3n l\u00fd t\u1ec7p, ki\u1ec3m tra tr\u1ea1ng th\u00e1i h\u1ec7 th\u1ed1ng v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c quy tr\u00ecnh kh\u00e1c m\u00e0 b\u1ea1n th\u01b0\u1eddng l\u00e0m th\u1ee7 c\u00f4ng. Nh\u1edd v\u00e0o kh\u1ea3 n\u0103ng t\u1ef1 \u0111\u1ed9ng h\u00f3a n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 ti\u1ebft ki\u1ec7m th\u1eddi gian, gi\u1ea3m thi\u1ec3u sai s\u00f3t v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<h3><b>Python c\u00f3 ph\u1ea3i l\u00e0 Bash script kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Kh\u00f4ng, Python v\u00e0 Bash script l\u00e0 hai lo\u1ea1i script kh\u00e1c nhau.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Bash script l\u00e0 t\u1eadp h\u1ee3p c\u00e1c l\u1ec7nh trong Bash shell, gi\u00fap th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 h\u1ec7 th\u1ed1ng v\u00e0 t\u1ef1 \u0111\u1ed9ng h\u00f3a quy tr\u00ecnh tr\u00ean c\u00e1c h\u1ec7 \u0111i\u1ec1u h\u00e0nh Unix-like. Python, ng\u01b0\u1ee3c l\u1ea1i, l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh \u0111a n\u0103ng v\u00e0 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho nhi\u1ec1u m\u1ee5c \u0111\u00edch, bao g\u1ed3m l\u1eadp tr\u00ecnh web, ph\u00e2n t\u00edch d\u1eef li\u1ec7u v\u00e0 ph\u00e1t tri\u1ec3n ph\u1ea7n m\u1ec1m.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1eb7c d\u00f9 c\u1ea3 hai \u0111\u1ec1u c\u00f3 th\u1ec3 d\u00f9ng \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a, nh\u01b0ng Python th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho c\u00e1c \u1ee9ng d\u1ee5ng ph\u1ee9c t\u1ea1p h\u01a1n so v\u1edbi Bash script, v\u1ed1n \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf cho c\u00e1c t\u00e1c v\u1ee5 h\u1ec7 th\u1ed1ng c\u01a1 b\u1ea3n.<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"\/blog\/python-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Python l\u00e0 g\u00ec: T\u1ed5ng quan \u0111\u1ecbnh ngh\u0129a, C\u00fa ph\u00e1p v\u00e0 Th\u01b0 vi\u1ec7n Python<\/strong><\/a><\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><b>T\u1ed5ng k\u1ebft<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Bash Script l\u00e0 c\u00f4ng c\u1ee5 kh\u00f4ng th\u1ec3 thi\u1ebfu \u0111\u1ed1i v\u1edbi nh\u1eefng ng\u01b0\u1eddi l\u00e0m vi\u1ec7c trong m\u00f4i tr\u01b0\u1eddng Unix-like. ITviec hy v\u1ecdng b\u00e0i vi\u1ebft \u0111\u00e3 cung c\u1ea5p cho b\u1ea1n ki\u1ebfn th\u1ee9c t\u1ed5ng quan v\u1ec1 Bash script. Vi\u1ec7c hi\u1ec3u r\u00f5 c\u00e1c kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n v\u00e0 k\u1ef9 thu\u1eadt x\u1eed l\u00fd l\u1ed7i s\u1ebd gi\u00fap b\u1ea1n ph\u00e1t tri\u1ec3n nh\u1eefng script m\u1ea1nh m\u1ebd v\u00e0 hi\u1ec7u qu\u1ea3. <\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash script l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng hi\u1ec7u qu\u1ea3, th\u00f4ng qua c\u00fa ph\u00e1p d\u1ec5 hi\u1ec3u v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng cao. Khi hi\u1ec3u th\u00e0nh th\u1ea1o Bash script, b\u1ea1n c\u00f3 th\u1ec3 x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p nhanh ch\u00f3ng v\u00e0 hi\u1ec7u qu\u1ea3 [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":86174,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-85892","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3 - ITviec Blog<\/title>\n<meta name=\"description\" content=\"T\u00ecm hi\u1ec3u chi ti\u1ebft Bash Script l\u00e0 g\u00ec, c\u00e1ch ho\u1ea1t \u0111\u1ed9ng, \u01b0u nh\u01b0\u1ee3c \u0111i\u1ec3m, h\u00e0m v\u00e0 c\u00e1c tips h\u1eefu \u00edch \u0111\u1ec3 n\u00e2ng cao k\u1ef9 n\u0103ng l\u1eadp tr\u00ecnh.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3\" \/>\n<meta property=\"og:description\" content=\"Bash script l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng hi\u1ec7u qu\u1ea3, th\u00f4ng qua c\u00fa ph\u00e1p d\u1ec5 hi\u1ec3u v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng cao. Khi hi\u1ec3u\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/bash-script-la-gi\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-21T02:16:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-17T02:46:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/bash-script-la-gi-vippro-scaled.png\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"337\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"M\u1ef9 Duy\u00ean\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"M\u1ef9 Duy\u00ean\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3 - ITviec Blog","description":"T\u00ecm hi\u1ec3u chi ti\u1ebft Bash Script l\u00e0 g\u00ec, c\u00e1ch ho\u1ea1t \u0111\u1ed9ng, \u01b0u nh\u01b0\u1ee3c \u0111i\u1ec3m, h\u00e0m v\u00e0 c\u00e1c tips h\u1eefu \u00edch \u0111\u1ec3 n\u00e2ng cao k\u1ef9 n\u0103ng l\u1eadp tr\u00ecnh.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/","og_locale":"vi_VN","og_type":"article","og_title":"Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3","og_description":"Bash script l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng hi\u1ec7u qu\u1ea3, th\u00f4ng qua c\u00fa ph\u00e1p d\u1ec5 hi\u1ec3u v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng cao. Khi hi\u1ec3u","og_url":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-04-21T02:16:06+00:00","article_modified_time":"2025-06-17T02:46:23+00:00","og_image":[{"width":640,"height":337,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/bash-script-la-gi-vippro-scaled.png","type":"image\/png"}],"author":"M\u1ef9 Duy\u00ean","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"M\u1ef9 Duy\u00ean","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"17 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3","datePublished":"2025-04-21T02:16:06+00:00","dateModified":"2025-06-17T02:46:23+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/"},"wordCount":4687,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/bash-script-la-gi-vippro-scaled.png","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/","url":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/","name":"Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3 - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/bash-script-la-gi-vippro-scaled.png","datePublished":"2025-04-21T02:16:06+00:00","dateModified":"2025-06-17T02:46:23+00:00","description":"T\u00ecm hi\u1ec3u chi ti\u1ebft Bash Script l\u00e0 g\u00ec, c\u00e1ch ho\u1ea1t \u0111\u1ed9ng, \u01b0u nh\u01b0\u1ee3c \u0111i\u1ec3m, h\u00e0m v\u00e0 c\u00e1c tips h\u1eefu \u00edch \u0111\u1ec3 n\u00e2ng cao k\u1ef9 n\u0103ng l\u1eadp tr\u00ecnh.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/bash-script-la-gi\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/bash-script-la-gi-vippro-scaled.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/bash-script-la-gi-vippro-scaled.png","width":640,"height":337,"caption":"bash script la gi - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/bash-script-la-gi\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"Bash script l\u00e0 g\u00ec? C\u00e1ch ch\u1ea1y Bash script hi\u1ec7u qu\u1ea3"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7","name":"M\u1ef9 Duy\u00ean","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","caption":"M\u1ef9 Duy\u00ean"},"url":"https:\/\/itviec.com\/blog\/author\/my-duyen\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/85892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/209"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=85892"}],"version-history":[{"count":1,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/85892\/revisions"}],"predecessor-version":[{"id":88179,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/85892\/revisions\/88179"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/86174"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=85892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=85892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=85892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}